mcp

← All adapters

import { mcp } from '@routecraft/ai'

Expose capabilities as MCP tools or call remote MCP servers. Requires mcpPlugin() in your context plugins when used as a source.

Source mode -- define a discoverable MCP tool:

The tool name is the route id; the tool's title, description, and schemas live on the route builder (enforced framework-wide). Only MCP-protocol extras (annotations, icons) remain on mcp() itself.

import { mcp } from '@routecraft/ai'
import { z } from 'zod'

craft()
  .id('fetch-webpage')
  .title('Fetch webpage')
  .description('Fetch the content of a webpage')
  .input({ body: z.object({ url: z.string().url() }) })
  .output({ body: z.object({ content: z.string() }) })
  .from(mcp({ annotations: { readOnlyHint: true, openWorldHint: true } }))
  .transform(async ({ url }) => {
    const res = await fetch(url)
    return { content: await res.text() }
  })

A non-empty .description() is required for every MCP source route (surfaced as the tool description in tools/list); the route fails to subscribe otherwise. The tool name (route id) is validated against the MCP interop regex ^[A-Za-z0-9_-]{1,64}$.

Destination mode -- call a remote MCP tool:

// Recommended: by server id registered in mcpPlugin({ clients }).
// Auth is inherited from the client config automatically.
.enrich(mcp('browser:browser_navigate', { args: (ex) => ({ url: ex.body.url }) }))

// By URL and tool name (use inline auth if needed)
.enrich(mcp({ url: 'http://127.0.0.1:8089/mcp', tool: 'browser_navigate' }, { args: (ex) => ({ url: ex.body.url }) }))

When using the serverId path (recommended), auth configured on the client in mcpPlugin({ clients }) flows to the destination automatically. Inline auth on McpClientOptions is available as an escape hatch for the raw url path or to override registered config, but prefer centralizing credentials in the plugin config.

Options (McpServerOptions -- source, protocol extras only):

OptionTypeRequiredDescription
annotationsMcpToolAnnotationsNoBehavior hints forwarded to MCP clients in the tools/list response
iconsMcpToolIcon[]NoIcons forwarded on tools/list per the MCP spec

All other tool metadata (title, description, input / output schemas) comes from the route builder and is enforced framework-wide:

Builder methodMaps toNotes
.id('tool-name')tool.nameValidated against ^[A-Za-z0-9_-]{1,64}$ at subscribe
.title('...')tool.titleOptional display title
.description('...')tool.descriptionRequired for MCP source routes
.input({ body, headers })tool.inputSchema + runtime checkbody validation is framework-enforced; headers validated values merge over the originals
.output({ body, headers })tool.outputSchema + runtime checkFramework-enforced before the primary destination fires

McpToolAnnotations (optional hint fields, all booleans unless noted):

These mirror the MCP specification (2025-03-26) ToolAnnotations shape. They are hints only; clients must not rely on them for correctness or safety.

FieldTypeDescription
titlestringHuman-readable title for the tool (used for display in UIs).
readOnlyHintbooleanWhen true, the tool does not modify any state. Clients assume false when omitted.
destructiveHintbooleanWhen true, the tool may perform destructive operations. Clients assume true when omitted.
idempotentHintbooleanWhen true, calling the tool repeatedly with the same arguments has no additional effect. Clients assume false when omitted.
openWorldHintbooleanWhen true, the tool may interact with external systems (network, filesystem, etc.). Clients assume true when omitted.

Options (McpClientOptions -- destination):

OptionTypeRequiredDescription
urlstringOne of url/serverIdDirect HTTP URL of the remote MCP server
serverIdstringOne of url/serverIdNamed server registered via mcpPlugin({ clients })
toolstringNoTool name to invoke (or set exchange.body.tool)
args(exchange) => Record<string, unknown>NoExtractor for tool arguments; defaults to exchange.body
authMcpClientAuthOptionsNoAuth credentials for HTTP requests. Auto-inherited from mcpPlugin({ clients }) when using serverId; use to override or for inline url connections

McpClientAuthOptions:

FieldTypeDescription
tokenstring | string[] | (() => string | Promise<string>)Bearer token, array of tokens (round-robin), or provider function called per request
headersRecord<string, string>Additional request headers; overrides token if Authorization is set

Tool Registry

Each .from(mcp(...)) route registers in MCP_LOCAL_TOOL_REGISTRY so the MCP server can list and invoke it via the MCP protocol:

import { MCP_LOCAL_TOOL_REGISTRY } from '@routecraft/ai'

const ctx = await new ContextBuilder().routes(...).build()
await ctx.start()

const registry = ctx.getStore(MCP_LOCAL_TOOL_REGISTRY)
const tools = registry ? Array.from(registry.values()) : []
// [{ endpoint, title?, description, input?, output?, annotations?, icons?, handler }]

mcp() and direct() maintain separate, fully isolated registries. An MCP route with .id('foo').from(mcp()) and a direct route with .id('bar').from(direct()) both register by their own ids in their own stores; direct routes never appear in the MCP tools/list response.

See Running an MCP server, Calling an MCP, and the MCP example.