trpc-mcp
by: Jacse
Serve tRPC routes as an MCP server
📌Overview
Purpose: To serve tRPC routes via the Model Context Protocol (MCP) for improved API handling and integration.
Overview: This framework enables seamless integration of tRPC routes with the MCP, allowing developers to leverage the flexibility of tRPC while benefiting from the structured communication that MCP provides. It aims to enhance the way APIs are built and consumed, promoting interoperability across different systems.
Key Features:
-
Meta Initialization: Integrates MCP metadata in tRPC services, allowing customization of API behavior and documentation.
-
Route Configuration: Enables the creation of procedural routes with structured input and output, enhancing clarity and predictability in API interactions.
-
Server Connection: Facilitates the establishment of a robust server connection using standard transport mechanisms, ensuring reliable communication between clients and tRPC services.
tRPC <-> MCP
Serve tRPC routes via MCP.
Usage
1. Add to meta
import { initTRPC } from '@trpc/server';
import { type McpMeta } from 'trpc-to-openapi';
const t = initTRPC.meta<McpMeta>().create();
2. Enable for routes
export const appRouter = t.router({
sayHello: t.procedure
.meta({ openapi: { enabled: true, description: 'Greet the user' } })
.input(z.object({ name: z.string() }))
.output(z.object({ greeting: z.string() }))
.query(({ input }) => {
return { greeting: `Hello ${input.name}!` };
});
});
3. Serve
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { createMcpServer } from 'trpc-mcp';
const mcpServer = createMcpServer(
{ name: 'trpc-mcp-example', version: '0.0.1' },
appRouter,
);
const transport = new StdioServerTransport();
await mcpServer.connect(transport);