UNPKG

@tanstack/ai-mcp

Version:

Host-side Model Context Protocol client for TanStack AI: discover and run MCP server tools, resources, and prompts in any adapter's chat() loop, with generated end-to-end types.

52 lines (51 loc) 2.75 kB
import { Client } from '@modelcontextprotocol/sdk/client/index.js'; import { Tool as McpToolDef } from '@modelcontextprotocol/sdk/types.js'; import { ContentPart } from '@tanstack/ai'; import { McpServerTool, McpToolMetadata } from './types.js'; interface ConvertOptions { prefix?: string; lazy?: boolean; } /** Reads the MCP Apps `_meta.ui.resourceUri` link from a tool def, if present. */ export declare function extractUiResourceUri(def: McpToolDef): string | undefined; /** * Build the `metadata.mcp` block stamped onto every discovered/bound tool. * Shared by auto-discovery (`toServerTools`) and the explicit `tools(defs)` * path in `client.ts` so the two cannot drift. * * `annotations` is the server's own object, forwarded verbatim. Per the MCP * spec its fields (including `title`) are **hints** — a host may use them for * display or to shape an approval UI, but never as a security boundary. * * Fields the server didn't declare are OMITTED rather than set to `undefined`: * the explicit path merges this over any `mcp` block the caller already put on * their tool definition, and an `undefined` value would blank out what they set. */ export declare function toolMcpMetadata(def: McpToolDef, serverId: string | undefined): McpToolMetadata; export declare function mcpContentToTanstack(content: Array<any>): string | Array<ContentPart>; /** * Build the execute body that proxies a TanStack tool call to an MCP server's * `callTool`. Shared by auto-discovery and the definition path. * * @param preferStructured when true (i.e. the tool declares an outputSchema), * return `result.structuredContent` if present so the existing output * validation in `executeServerTool` validates MCP's typed payload rather than * a JSON-in-text blob. Otherwise normalize `content[]` → string | ContentPart[]. */ export declare function makeMcpExecute(client: Client, mcpName: string, preferStructured: boolean): (args: unknown, ctx?: { abortSignal?: AbortSignal; }) => Promise<{} | null>; /** * A tool with `execution.taskSupport: 'required'` can only run through the * SDK's experimental task-based execution (`tasks/callToolStream`) — plain * `callTool` is rejected by the server with -32600. Until task execution is * supported, such tools must not be offered to the model. */ export declare function requiresTaskExecution(def: McpToolDef): boolean; /** * Auto-discovery path: turn raw MCP tool defs into ServerTools (args typed * `unknown`). Task-required tools are excluded — they cannot be invoked via * plain `callTool` (see {@link requiresTaskExecution}). */ export declare function toServerTools(client: Client, defs: Array<McpToolDef>, options: ConvertOptions): Array<McpServerTool>; export {};