
iboughtbed/trpc-to-mcp
API developmentЭта страница переведена автоматически. Технические детали смотрите в оригинале. Открыть оригинал
tRPC to MCP package
Обзор
You can skip this part if you know exactly why you're turning your tRPC endpoints into MCP tools. At its core, the Model Context Protocol (MCP) gives language models a context to interact with external systems by exposing well-defined tools. This isn't about wrapping API endpoints- it's about enabling AI to perform tasks with clarity and purpose. This library was created to convert only the tRPC procedures you explicitly choose into MCP tools. By doing so, it ensures that only the functionalities intended for controlled AI interaction are exposed, rather than indiscriminately converting all endpoints. Remember, for an endpoint to be effectively transformed into an MCP tool, it should be meticulously documented with clear descriptions, meaningful names, and well-defined inputs/outputs. This careful design is crucial because MCP tools are more than mere API endpoints - they form the operational contexts that enable language models to function reliably.
README
Показан фрагмент оригинального README. Открыть оригинал.
trpc-to-mcp
Turn the tRPC procedures you choose into MCP tools.
- Works with tRPC v11 and
@modelcontextprotocol/serverv2, the MCP TypeScript SDK - Serve tools over stdio or HTTP
- Use the tRPC context and middlewares on every tool call
- Transform outputs into text, images, or other MCP content
- Return structured content from procedures with an output schema
- Mount on Next.js and other frameworks with mcp-handler
Install
pnpm add trpc-to-mcp @modelcontextprotocol/server
@trpc/server 11 and @modelcontextprotocol/server 2 are peer dependencies. Install mcp-handler 2 only if you use the mcp-handler adapter. Node.js 20 or later is required.
Usage
- Add
McpMetato your tRPC instance.
import { initTRPC } from "@trpc/server";
import { type McpMeta } from "trpc-to-mcp";
const t = initTRPC.context().meta().create();
- Enable
mcpon the procedures you want as tools.
import { z } from "zod";
export const appRouter = t.router({
hello: t.procedure
.meta({
mcp: {
enabled: true,
description: "Say hello to someone",
// Optional. Defaults to the procedure path with dots replaced by
// underscores, e.g. "hello" or "user_byId".
name: "say_hello",
},
})
.input(z.object({ name: z.string().describe("Who to greet") }))
.query(({ input }) => `Hello ${input.name}`),
});
Inputs must be object schemas from a validator that implements Standard JSON Schema, such as zod 4.2 or later, ArkType, or Valibot with toStandardJsonSchema(). Chained .input() calls are merged into one tool input schema. Subscriptions can’t be tools.
tRPC validates input on every call, so your zod transforms and refinements run as usual. Errors thrown by a procedure, including TRPCError, come back to the client as a tool result with isError: true.
meta.mcp also takes title and annotations. Queries get readOnlyHint: true unless you set it yourself.
Serve your tools
createMcpServer returns an McpServer with your tools registered. You can register more tools, prompts, and resources on it.
Over stdio:
import { serveStdio } from "@modelcontextprotocol/server/stdio";
import { createMcpServer } from "trpc-to-mcp";
serveStdio(() =>
createMcpServer({ name: "my-app", version: "1.0.0" }, appRouter, { db }),
);
Установка
This server does not publish a one-line install command.
Open the repository installation guide Новичок в MCP? Начните с руководства на mcpskill.org →