Back to MCP directory
publicPublicdnsLocal runtime

trpc-mcp

通过MCP服务提供tRPC路由功能,实现RPC接口的开放与调用

article

README

🚀 tRPC 与 MCP 之间的连接

本项目通过 MCP 提供 tRPC 路由,实现了 tRPC 与 MCP 之间的有效连接,为服务的交互和调用提供了便利。

🚀 快速开始

💻 使用示例

基础用法

以下是实现 tRPC 与 MCP 连接的具体步骤:

第一步:在元数据中导入

import { initTRPC } from '@trpc/server';
import { type McpMeta } from 'trpc-to-openapi';

const t = initTRPC.meta<McpMeta>().create();

第二步:为路由启用 MCP 支持

export const appRouter = t.router({
  sayHello: t.procedure
    .meta({ openapi: { enabled: true, description: '向用户问好' } })
    .input(z.object({ name: z.string() }))
    .output(z.object({ greeting: z.string() }))
    .query(({ input }) => {
      return { greeting: `你好 ${input.name}!` };
    });
});

第三步:提供服务

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);
help

Runtime guide

cloud

Hosted runtime

Hosted servers run from a provider-managed environment. You usually connect the MCP client to the hosted endpoint or follow the provider's authorization flow, without keeping a local process alive

  1. Open provider connection page
  2. Authorize or copy endpoint
  3. Connect from your MCP client
terminal

Local runtime / other methods

Local servers run on your own machine or infrastructure. You normally copy the server_config into your MCP client, install the required package, and provide env variables from env_schema when needed

  1. Copy server_config
  2. Install required package
  3. Fill env variables and restart client