import type * as http from 'node:http'; import type { Agent, ToolsInput } from '@mastra/core/agent'; import { MCPServerBase } from '@mastra/core/mcp'; import type { MCPServerConfig as CoreMCPServerConfig, MCPToolExecutionResultV2, ServerDetailInfo, ServerInfo } from '@mastra/core/mcp'; import { RequestContext } from '@mastra/core/request-context'; import type { InternalCoreTool, MCPToolType } from '@mastra/core/tools'; import type { Workflow } from '@mastra/core/workflows'; import type { Resource, jsonSchemaValidator } from '@modelcontextprotocol/server'; import { ServerPromptActions, ServerResourceActions, ServerToolActions } from './actions.js'; import type { AppResources, MCPServerCacheHints, MCPServerHTTPRequestOptions, MCPServerPrompts, MCPServerRequestStateOptions, MCPServerResources } from './types.js'; export interface MCPServerConfig extends CoreMCPServerConfig { resources?: MCPServerResources; prompts?: MCPServerPrompts; /** MCP Apps (SEP-1865) HTML resources served under `ui://`. */ appResources?: AppResources; /** Custom JSON Schema validator, for runtimes where the SDK default is unavailable. */ jsonSchemaValidator?: jsonSchemaValidator; cacheHints?: MCPServerCacheHints; /** Integrity protection for `input_required` continuation state. */ requestState?: MCPServerRequestStateOptions; } export interface MCPServerHTTPOptions { url: URL; httpPath: string; req: http.IncomingMessage; res: http.ServerResponse; options?: MCPServerHTTPRequestOptions; } /** Tool description served over Mastra's REST routes; `id` is what Studio keys tools by. */ type ToolInfo = { id: string; name: string; description?: string; inputSchema: Record; outputSchema?: Record; toolType?: MCPToolType; _meta?: Record; }; /** * Exposes Mastra tools, agents, workflows, resources and prompts to Model Context * Protocol (MCP) clients using protocol revision 2026-07-28: self-contained requests * over Streamable HTTP or stdio, native `input_required` continuation and * per-request logging. * * @example * `yourTool` is a tool you have already configured. * ```typescript * import { MCPServer } from '@mastra/mcp'; * * const server = new MCPServer({ * id: 'my-server', * name: 'My Server', * version: '1.0.0', * tools: { yourTool }, * }); * await server.startStdio(); * ``` * * @see For documentation bundled with your installed package, locate * `@mastra/mcp/package.json` with your project's resolver or package-manager * tooling, then read `dist/docs/SKILL.md` from that package root and follow its * reference links. Use package-manager tools for virtual or archived packages. * * @see [MCP server documentation](https://mastra.ai/reference/tools/mcp-server) * if packaged docs are unavailable. */ export declare class MCPServer extends MCPServerBase { readonly mcpVersion: 2; readonly resources: ServerResourceActions; readonly prompts: ServerPromptActions; readonly toolActions: ServerToolActions; private readonly resourceOptions?; private readonly promptOptions?; private readonly appResourceList; private readonly appResourceHtml; private readonly jsonSchemaValidator?; private readonly cacheHints?; private readonly mapAuthInfoToUser?; private readonly fga?; private readonly requestStateCodec; private httpHandler?; private nodeHandler?; private stdioHandle?; private stdioInstance?; constructor(config: MCPServerConfig); private createRequestStateCodec; convertTools(tools: ToolsInput, agents?: Record, workflows?: Record): Record; /** * A suspended handler's `resumeSchema` becomes the form the client fills in, and * the protocol only allows a flat object of primitives there. Checked at registration * so a tool cannot suspend into a request no client can answer. */ private assertFormRepresentable; /** * Input-request forms are a restricted flat-object subset of JSON Schema, so * the dialect declaration is left off the requested schema. */ private formSchema; /** * Converts a tool schema to JSON Schema 2020-12, the dialect MCP 2026-07-28 * assumes when none is declared. The dialect declaration is kept so validators * that dispatch on `$schema` pick the same draft on both sides. Some vendors * (e.g. zod v3) emit 2019-09 for this target, so it is rewritten to 2020-12. */ private jsonSchema; private addTools; private removeTools; /** Mirrors `__registerMastra`: the tool's intrinsic id when it has one, else its key. */ private mastraToolKey; /** Schema-less tools advertise an open empty object rather than the default validator schema. */ private hasInputSchema; private resumeSchemaOf; /** * Prefers the tool's own schema over the converted core tool's, which has * already been lowered to draft-07, so 2020-12 shapes survive advertisement. */ private advertisedSchema; private toolInfo; /** Builds the wire tool description, validated against the spec schema. */ private toMCPTool; private hasUiMetadata; private capabilities; private createServerInstance; /** * Registers `method` under an `MCP_SERVER_REQUEST` span, so every request the * server answers is recorded — including the list and read methods that never * reach a tool, and requests that fail before any work is done. */ private setTracedHandler; /** * Opens the span for one served request. `connection` is absent for * {@link executeTool}, which is called in-process and negotiates nothing. */ private startRequestSpan; /** * Runs `fn` under a request span. Only the protocol's own `isError` marks a * failure; the rest of a result is opaque handler output that may legitimately * carry any field. A thrown error ends the whole span tree and is rethrown. */ private traceRequest; /** Answers a suspended round: one keyed form derived from the handler's `resumeSchema`. */ private inputRequired; private serverRequest; private registerToolHandlers; /** * Runs one round of a tool. The tool sees the same `suspend` / `resumeData` / * `suspendPayload` vocabulary as under an agent or workflow; nothing else is * carried between rounds. */ private runTool; private toCallToolResult; private registerResourceHandlers; private resourceContents; private registerPromptHandlers; private loadAppResources; private enforceToolExecutionFGA; private authorizedToolEntries; private notifier; private getNodeHandler; /** Serves the current process's stdio. Legacy openings are rejected. */ startStdio(): Promise; /** * Handles one Streamable HTTP request at `httpPath`. Every request is * self-contained; requests without a 2026-07-28 envelope are rejected. */ startHTTP({ url, httpPath, req, res, options }: MCPServerHTTPOptions): Promise; close(): Promise; getServerInfo(): ServerInfo; getServerDetail(): ServerDetailInfo; getToolListInfo(requestContext?: RequestContext): { tools: ToolInfo[]; } | Promise<{ tools: ToolInfo[]; }>; getToolInfo(toolId: string): ToolInfo | undefined; /** * Runs a tool without a protocol client (the Studio/REST route). A tool that * suspends is reported as such; the caller continues by sending the same * arguments with `resumeData` and `suspendPayload`. */ executeTool(toolId: string, args: unknown, executionContext?: Parameters[2]): Promise; /** Reads an `ui://` app resource; application resources require a protocol request. */ readResource(uri: string): Promise<{ contents: Array<{ uri: string; text?: string; blob?: string; mimeType?: string; _meta?: Record; }>; }>; /** Lists `ui://` app resources; application resources require a protocol request. */ listResources(): Promise<{ resources: Resource[]; }>; } export { ServerPromptActions, ServerResourceActions, ServerToolActions }; //# sourceMappingURL=server.d.ts.map