npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

trpc-to-mcp

v1.3.2

Published

tRPC to MCP adapter

Downloads

1,621

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

Usage

  1. Install trpc-to-mcp
npm install trpc-to-mcp

pnpm add trpc-to-mcp

bun add trpc-to-mcp

yarn add trpc-to-mcp
  1. Add McpMeta to your tRPC instance
import { initTRPC } from "@trpc/server";
import { type McpMeta } from "trpc-to-mcp";

const t = initTRPC.meta<McpMeta>().create();
  1. Enable mcp support 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

  1. 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

  1. 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

  1. You can turn a router into @vercel/mcp-adapter handler
// 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

  1. 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 :)