Skip to content

Add an optional durableObjectName field to serve/serveSSE methods for persistent routing across MCP sessions #536

@alex-ottex

Description

@alex-ottex

Current implementation of McpAgent creates a new DO for each new session, which greatly diminishes the scope of MCPs we can build with the McpAgent class.

  • no option to connect multiple clients simultaneously to one MCP instance
  • no option to persist state between sessions

const agent = await getAgentByName(

I would even say this hardcoded getAgentName scoped by sessionId goes against the idea of using DO as a backend for MCPs, since it gives an option to create only ephemeral session scoped MCP servers which is not different from just a worker with MCP interface.

What if I want to give my user a persistent MCP URL that will preserve state between sessions? e.g. A database for agent memory, agent notes, agent tasks...

As a developer, I want an option to control an instance of the DO object to which the request is going to be routed. It's okay to have a default implementation, but I think it's a good idea to provide an optional property to the serve functions.

From a DX perspective, the code to route requests to a specific DO can look like this:

export default {
  async fetch(request: Request, env: any, ctx: ExecutionContext): Promise<Response> {
    const url = new URL(request.url);
    const objectId = url.searchParams.get("id");

    if (!objectId) {
          return new Response("MCP ID is missing", { status: 404 });
    }

    // Route MCP endpoint to MCP agent
    if (url.pathname === "/mcp" || url.pathname.startsWith("/mcp/")) {
      return MyMCP.serve("/mcp", {durableObjectName: objectId}).fetch(request, env, ctx);
    }

    return new Response("Not found", { status: 404 });
  },

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions