trpc-to-mcp
v1.3.2
Published
tRPC to MCP adapter
Downloads
1,621
Maintainers
Readme
Introduction
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.
tRPC to MCP
- Support for tRPC v11
- Transform your JSON outputs into human-language with custom helpers
- Turn your tRPC router into MCP tools
- Turn your tRPC router into MCP server
- Turn your tRPC router into @vercel/mcp-adapter handler
Usage
- Install trpc-to-mcp
npm install trpc-to-mcp
pnpm add trpc-to-mcp
bun add trpc-to-mcp
yarn add trpc-to-mcp- Add
McpMetato your tRPC instance
import { initTRPC } from "@trpc/server";
import { type McpMeta } from "trpc-to-mcp";
const t = initTRPC.meta<McpMeta>().create();- Enable
mcpsupport for a procedure
export const appRouter = t.router({
hello: t.procedure
.meta({
mcp: {
enabled: true,
description: "Say hello to a name",
// You can leave it empty, it'll be converted to "hello"
// If you have subrouters, then subrouter_procedure_name
name: "say_hello",
},
})
.input(
z.object({
name: z.string(),
}),
)
.query(({ input }) => {
return { greeting: `Hello ${input.name}` };
}),
});Transform Outputs
If you need a human-language outputs for LLMs to have a better context, you can use transformMcpProcedure
You simply wrap your procedure with the helper function, and provide a transformer callback function that turns your procedure output into ContentBlock[] array compatible with MCP content output.
import { initTRPC } from "@trpc/server";
import { z } from "zod";
import { transformMcpProcedure, type McpMeta } from "../src";
const t = initTRPC.meta<McpMeta>().create();
export const appRouter = t.router({
procedure: transformMcpProcedure(
t.procedure
.meta({
mcp: {
enabled: true,
description: "Send a message",
name: "send_message",
},
})
.input(
z.object({
message: z.string(),
}),
)
.query(({ input }) => {
return {
payload: {
from: "trpc",
message: input.message,
array: [{ a: 1 }, { b: 2 }],
},
};
}),
(output) => {
return [
...output.payload.array.map((item) => {
const [name, value] = Object.entries(item);
return {
type: "text" as const,
text: `${name} is ${value} letter of alphabet`,
};
}),
{
type: "image",
data: "...",
mimeType: "",
},
{
type: "audio", // or even "resource" | "resource_link"
data: "...",
mimeType: "",
},
];
},
),
});To MCP tools
- You can turn a router into tools
import { appRouter } from "@/server/trpc/root.ts";
import { extractToolsFromProcedures } from "trpc-to-mcp";
const tools = extractToolsFromProcedures(appRouter);
/*
[
{
name: "Tool name",
description: "Description of the tool",
pathInRouter: [...],
inputSchema: JSONSchema // this if full JSON schema from your zod schema
},
...
]
*/To MCP server
- You can turn a router into an MCP server
import { appRouter } from "@/server/trpc/root.ts";
import { createMcpServer } from "trpc-to-mcp";
const ctx = {
session: {
// ...
},
};
// Returns high-level McpServer instance
const mcpServer = createMcpServer(
implementation, // Pass your Implementation instance
appRouter,
ctx, // Pass your tRPC context
);To Vercel MCP adapter
- You can turn a router into
@vercel/mcp-adapterhandler
// app/api/[transport]/route.ts
import { trpcToMcpHandler } from "trpc-to-mcp/adapters/vercel-mcp-adapter";
const handler = trpcToHandler(appRouter, ctx, {
// @vercel/mcp-adapter config options
config: {
basePath: "/api",
// Disable in production
verboseLogs: true,
maxDuration: 60,
},
// @vercel/mcp-adapter server options
serverOptions: { ... },
callback: (server) => {
// You can modify the McpServer instance
}
});
export { handler as DELETE, handler as GET, handler as POST };Use with better-auth
- You can use it with better-auth
// app/api/[transport]/route.ts
import { withMcpAuth } from "better-auth/plugins";
import { trpcToMcpHandler } from "trpc-to-mcp/adapters/vercel-mcp-adapter";
// Define a function to retrieve user session from database
async function getSession(mcpSession: McpSession): Promise<Session | null> {
// You can use mcpSession.userId to fetch session
// await db.query.session.findFirst({
// where: (table, { eq }) => eq(table.userId, mcpSession.userId)
// })
}
const handler = withMcpAuth(auth, async (request, mcpSession) => {
// Retrieve the session from database via userId inside mcpSession
const session = await getSession(mcpSession);
const handler = trpcToMcpHandler(
appRouter,
// Pass your tRPC context here
{
db,
headers: request.headers,
session: session?.session,
user: session?.user,
},
{
config: {
basePath: "/api",
// Disable in production
verboseLogs: true,
maxDuration: 60,
},
},
);
return await handler(request);
});
export { handler as DELETE, handler as GET, handler as POST };Result
Now you can chill and add MCP to your VC-backed startup so your investors won't worry, and profit
Twitter / X
https://x.com/iboughtbed - cracked 17 year old engineer from Kazakhstan :)
