Skip to content

Add optional server filter to getAITools() #1073

@mattzcarey

Description

@mattzcarey

Problem

When an AIChatAgent connects to multiple MCP servers simultaneously (e.g. keeping OAuth connections alive across server switches), mcp.getAITools() returns tools from all connected servers with no way to scope to a specific one.

The current workaround is manual prefix filtering on the tool key:

private getActiveTools() {
  const activeName = this._mcpConnection.currentName;
  if (!activeName) return {};

  const activeServer = this.mcp.listServers().find((s) => s.name === activeName);
  if (!activeServer) return {};

  const allTools = this.mcp.getAITools();
  const prefix = `tool_${activeServer.id.replace(/-/g, "")}_`;
  return Object.fromEntries(
    Object.entries(allTools).filter(([key]) => key.startsWith(prefix))
  );
}

This relies on internal naming conventions (tool_<id-without-dashes>_<toolname>) which is fragile and shouldn't be something consumers need to know about.

Proposal

Add an optional filter parameter to getAITools():

// Filter by server ID
this.mcp.getAITools({ serverId: activeServer.id })

// Or filter by server name
this.mcp.getAITools({ serverName: "cloudflare" })

Use Case

On mcp.cloudflare.com, users can switch between 10+ MCP servers (Stripe, Sentry, Asana, etc.). We keep OAuth connections alive so users don't have to re-authenticate when switching back. But we only want to pass the active server's tools to the LLM — not tools from every server the user has ever connected to in that session.

Metadata

Metadata

Assignees

No one assigned

    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