Skip to content

Conversation

allenporter
Copy link
Contributor

@allenporter allenporter commented Dec 28, 2024

Proposed change

Add the Model Context Protocol Server integration.

See https://modelcontextprotocol.io/introduction#general-architecture for background on how MCP works.

This exposes an MCP server using the SSE Transport. This is an updated version of https://github.com/allenporter/home-assistant-model-context-protocol but with native SSE support, rather than websockets, now that Home Assistant core supports pydantic v2. This adds dependencies on the mcp python SDK, as well as aiohttp-sse/anyio for generating responses. Note: We can move more of http.py into a library if desired to push more SSE protocol details out of Home Assistant.

Protocol roadmap: https://modelcontextprotocol.io/development/roadmap -- the actual final definition for how remote MCP servers work is still in progress, as well as authentication.

Remaining work items in draft:

  • Update local MCP server to use new SSE protocol
  • End to end testing w/ Claude and another client
  • Add Claude desktop configuration to documentation

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

) -> None:
"""Test the tools list endpoint."""

async with mcp_session(mcp_sse_url, hass_supervisor_access_token) as session:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reviewer: This is using the MCP SDK client library for testing the MCP and SSE protocol for the integration end to end. This is not typical for a Home Assistant test, but since this is adding a new protocol it seemed to make sense. If desired, we can make the tests lower level.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this, because it guarantees that our implementation works instead of trying to keep our lower level tests in sync with the spec.

{LIGHT_DOMAIN: [{"platform": "test"}]},
)

async_expose_entity(hass, CONVERSATION_DOMAIN, TEST_ENTITY, True)
Copy link
Member

@balloob balloob Dec 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not to be solved in this PR, but we should come up with a better way to expose entities. We can't tie everything that is not Google/Amazon to conversation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, i don't really think i understand the product direction here at the moment for this being tied together.

Having Home Assistant as an MCP client also raises some other broader points: Do we add exposed tools to another API? or add them to Assist? Do we want to find a way to make discovered prompts usable with other models? e.g. revisit config sub entries.

It seems like there are more degrees of freedom generally that we'll want here and exposed entities is one example.

Copy link
Contributor Author

@allenporter allenporter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed comments. I have this working end to end and with mcp-proxy added features, so this is all ready to go.

@allenporter allenporter marked this pull request as ready for review December 29, 2024 22:52
"llm_hass_api": "[%key:common::config_flow::data::llm_hass_api%]"
},
"data_description": {
"llm_hass_api": "The method for controling Home Assistant to expose with the Model Context Protocol."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we translate this as "Control", as we do for LLMs?

Suggested change
"llm_hass_api": "The method for controling Home Assistant to expose with the Model Context Protocol."
"llm_hass_api": "Control"

(btw, I guess no control means really not much to be done)

Copy link
Contributor Author

@allenporter allenporter Dec 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're using [%key:common::config_flow::data::llm_hass_api%] above which is Control Home Assistant. Just to make sure: Are you making this comment for the data field or data_description field? This is for data_description and I didn't see that the LLM integrations have a data_description` field.

Let me know and I can address as a follow up.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes confused the two.

@balloob balloob merged commit a5d0c35 into home-assistant:dev Jan 2, 2025
65 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jan 3, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants