Skip to content

feat(mcp): add Model Context Protocol server integration#1451

Open
toubatbrian wants to merge 4 commits into
mainfrom
claude/port-mcp-tools-js-CXR2H
Open

feat(mcp): add Model Context Protocol server integration#1451
toubatbrian wants to merge 4 commits into
mainfrom
claude/port-mcp-tools-js-CXR2H

Conversation

@toubatbrian
Copy link
Copy Markdown
Contributor

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-agents library.

Changes Made

  • New MCP module (agents/src/llm/mcp.ts): Implements MCP client integration with support for:

    • MCPServer: Abstract base class for MCP server connections
    • MCPServerHTTP: HTTP-based transport supporting SSE and streamable HTTP protocols
    • MCPServerStdio: Stdio-based transport for spawning local MCP server processes
    • Lazy loading of the optional @modelcontextprotocol/sdk peer dependency
    • Configurable tool result resolution and client session timeouts
  • Agent-level MCP support (agents/src/voice/agent.ts):

    • Added mcpServers option to AgentOptions for agent-specific MCP server configuration
    • Agent-level MCP servers override session-level configuration
  • Session-level MCP support (agents/src/voice/agent_session.ts):

    • Added mcpServers option to AgentSessionOptions for session-wide MCP server configuration
    • Provides fallback MCP servers when agents don't specify their own
  • Activity-level MCP integration (agents/src/voice/agent_activity.ts):

    • Initializes and manages MCP servers when an activity starts
    • Merges MCP-provided tools with agent-defined tools in the tool context
    • Properly closes agent-scoped MCP servers on activity shutdown
    • Maintains separate tracking of MCP tools vs. agent-defined tools for proper tool updates
  • Public API exports (agents/src/llm/index.ts):

    • Exports all MCP-related types and classes for public use
  • Package configuration (agents/package.json):

    • Added @modelcontextprotocol/sdk as an optional peer dependency (v1.25.0+)
  • Example (examples/src/mcp_agent.ts):

    • Demonstrates basic usage of MCP servers with an agent session

Pre-Review Checklist

  • Build passes: All builds (lint, typecheck, tests) pass locally
  • AI-generated code reviewed: Removed unnecessary comments and ensured code quality
  • Changes explained: All changes are properly documented and justified above
  • Scope appropriate: All changes relate to MCP server integration
  • Video demo: Tested with Agent Playground (if applicable)

Testing

  • MCP module uses lazy imports to avoid breaking existing code when the SDK is not installed
  • Tool merging logic properly handles both agent-level and session-level MCP servers with correct precedence
  • MCP servers are properly initialized on activity start and closed on shutdown
  • Existing tool update mechanisms continue to work with MCP tools included

Additional Notes

  • @modelcontextprotocol/sdk is an optional peer dependency; users must install it explicitly to use MCP features
  • MCP tools are merged with agent-defined tools transparently; no changes needed to existing agent code
  • Agent-level MCP servers take precedence over session-level configuration, allowing per-agent customization

https://claude.ai/code/session_01F8JvYZcQXC2ci3hrCMiU1c

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-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 10, 2026

🦋 Changeset detected

Latest commit: 7ba7714

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 31 packages
Name Type
@livekit/agents Patch
@livekit/agents-plugin-anam Patch
@livekit/agents-plugin-assemblyai Patch
@livekit/agents-plugin-baseten Patch
@livekit/agents-plugin-bey Patch
@livekit/agents-plugin-cartesia Patch
@livekit/agents-plugin-cerebras Patch
@livekit/agents-plugin-deepgram Patch
@livekit/agents-plugin-elevenlabs Patch
@livekit/agents-plugin-fishaudio Patch
@livekit/agents-plugin-google Patch
@livekit/agents-plugin-hedra Patch
@livekit/agents-plugin-hume Patch
@livekit/agents-plugin-inworld Patch
@livekit/agents-plugin-lemonslice Patch
@livekit/agents-plugin-liveavatar Patch
@livekit/agents-plugin-livekit Patch
@livekit/agents-plugin-minimax Patch
@livekit/agents-plugin-mistral Patch
@livekit/agents-plugin-mistralai Patch
@livekit/agents-plugin-neuphonic Patch
@livekit/agents-plugin-openai Patch
@livekit/agents-plugin-phonic Patch
@livekit/agents-plugin-resemble Patch
@livekit/agents-plugin-rime Patch
@livekit/agents-plugin-runway Patch
@livekit/agents-plugin-sarvam Patch
@livekit/agents-plugin-silero Patch
@livekit/agents-plugins-test Patch
@livekit/agents-plugin-trugen Patch
@livekit/agents-plugin-xai Patch

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

@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 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 {
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Comment thread agents/src/llm/mcp.ts
Comment on lines +141 to +144
this._clientSessionTimeout =
options.clientSessionTimeout === undefined
? DEFAULT_CLIENT_SESSION_TIMEOUT
: options.clientSessionTimeout;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

devin-ai-integration[bot]

This comment was marked as resolved.

- 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
devin-ai-integration[bot]

This comment was marked as resolved.

`_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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants