feat(mcp): add Model Context Protocol server integration#1451
feat(mcp): add Model Context Protocol server integration#1451toubatbrian wants to merge 4 commits into
Conversation
Adds Model Context Protocol integration mirroring the Python implementation. New `MCPServer` base class with `MCPServerHTTP` (SSE + streamable HTTP) and `MCPServerStdio` transports, exposed via `@livekit/agents`'s `llm` namespace. `Agent` and `AgentSession` accept an `mcpServers` option; tools are fetched on activity start and merged into the active tool context. `@modelcontextprotocol/sdk` is added as an optional peer dependency so users without MCP do not need to install it. https://claude.ai/code/session_01F8JvYZcQXC2ci3hrCMiU1c
🦋 Changeset detectedLatest commit: 7ba7714 The changes in this PR will be included in the next version bump. This PR includes changesets to release 31 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 892b916766
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| @@ -647,7 +655,7 @@ export class AgentActivity implements RecognitionHooks { | |||
| } | |||
There was a problem hiding this comment.
Include MCP tools in pipeline generations
For non-realtime LLM sessions, generateReply still constructs the tool set from this.agent.toolCtx before calling pipelineReplyTask, so the MCP tools merged here are never passed to performLLMInference; only realtime sessions that read this.tools during _updateSession see them. This means the example/session path using new inference.LLM(...) with MCP servers will not advertise any MCP tools to the model and the tools cannot be called. Please make the pipeline path pass the merged tool context, preserving the existing on-enter filtering behavior.
Useful? React with 👍 / 👎.
| this._clientSessionTimeout = | ||
| options.clientSessionTimeout === undefined | ||
| ? DEFAULT_CLIENT_SESSION_TIMEOUT | ||
| : options.clientSessionTimeout; |
There was a problem hiding this comment.
Apply the advertised MCP timeouts
The new clientSessionTimeout option is documented as a per-request timeout and defaults to 5000 ms, but the stored value is never passed to the MCP client or used around listTools/callTool. In environments where an MCP server stops responding, users who rely on the default or pass a shorter timeout will still wait for the SDK/transport behavior instead of the documented limit. Please wire this option into the client/request calls or remove the advertised timeout.
Useful? React with 👍 / 👎.
- Pass merged tool context (agent + MCP tools) to pipelineReplyTask so non-realtime LLM sessions actually see MCP-provided tools. Previously generateReply read from agent.toolCtx, dropping the merged MCP tools. - Apply clientSessionTimeout to listTools/callTool requests via the SDK RequestOptions.timeout so the documented per-request timeout takes effect. - Drop unwired timeout / sseReadTimeout from MCPServerHTTPOptions; the JS MCP SDK has no clean per-call mapping, and they were silently ignored. https://claude.ai/code/session_01F8JvYZcQXC2ci3hrCMiU1c
`_detachReusableResources` compared `this.tools` against `newActivity.tools` to decide whether the realtime session can be reused. The new activity has not yet run `_initializeMcpServers`, so its `_mcpTools` is empty, making the comparison always report tools as different when MCP servers are configured and forcing unnecessary RT session recreation during handoffs (when the realtime model lacks `midSessionToolsUpdate`). Compare agent-level tools and MCP server references separately. Servers match when both lists hold the same `MCPServer` instances in order. https://claude.ai/code/session_01F8JvYZcQXC2ci3hrCMiU1c
Description
Adds support for Model Context Protocol (MCP) servers to the agents framework, enabling agents to dynamically fetch and use tools exposed by MCP servers. This ports MCP tool support from the Python
livekit-agentslibrary.Changes Made
New MCP module (
agents/src/llm/mcp.ts): Implements MCP client integration with support for:MCPServer: Abstract base class for MCP server connectionsMCPServerHTTP: HTTP-based transport supporting SSE and streamable HTTP protocolsMCPServerStdio: Stdio-based transport for spawning local MCP server processes@modelcontextprotocol/sdkpeer dependencyAgent-level MCP support (
agents/src/voice/agent.ts):mcpServersoption toAgentOptionsfor agent-specific MCP server configurationSession-level MCP support (
agents/src/voice/agent_session.ts):mcpServersoption toAgentSessionOptionsfor session-wide MCP server configurationActivity-level MCP integration (
agents/src/voice/agent_activity.ts):Public API exports (
agents/src/llm/index.ts):Package configuration (
agents/package.json):@modelcontextprotocol/sdkas an optional peer dependency (v1.25.0+)Example (
examples/src/mcp_agent.ts):Pre-Review Checklist
Testing
Additional Notes
@modelcontextprotocol/sdkis an optional peer dependency; users must install it explicitly to use MCP featureshttps://claude.ai/code/session_01F8JvYZcQXC2ci3hrCMiU1c