Serving MCP Servers Over stdout

    Matt PocockMatt Pocock
    Source Code

    I'm going to show you the simplest way you can set up an MCP server.

    We're going to create an MCP server, connect it to Claude Code, and then use it to run any script we want.

    And we're going to do this all with a single TypeScript file and no build step.

    Let's go.

    The Server

    We're going to start by creating a main.ts file.

    We're going to initialize an McpServer:

    import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
    const server = new McpServer({
    name: "Weather Service",
    version: "1.0.0",
    });

    We're getting this MCP server from the @modelcontextprotocol/sdk package.

    Scrollycoding

    1

    We can then add a tool to this server. We first define the name of the tool, getWeather.

    2

    Then we define what arguments the tool needs to receive in order to run. In this case, we only need the city, which is a string. And we specify this using Zod.

    3

    Finally, we add a callback function that only runs when the tool is called. We return an array of content objects, where in this case, we're returning text. And we say that the weather in that place is sunny.

    This is the function where you'd actually go and call the weather API if we were implementing this for real.

    server.tool("getWeather");

    Our server is actually complete. But we need some way for Claude Code to communicate with our server.

    It's going to do this by running this file and communicating with it via stdin.

    To get this working, we can use a StdioServerTransport from @modelcontextprotocol/sdk.

    We first define the transport, then connect the server to it.

    import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
    const transport = new StdioServerTransport();
    await server.connect(transport);

    Connecting to Claude Code

    Now in Claude Code, we can run claude mcp add. This starts an interactive dialogue where you can choose the server you want to connect to.

    Right now Claude Code only supports stdio transport - which is what we're using.

    In the video above, I walk through the interactive steps, but you can run this with a single command:

    claude mcp add "weather-example" npx tsx "/path-to-the-file.ts"

    This tells Claude that in order to run the file, it should call npx tsx /path-to-the-file.ts.

    npx lets us run any script from npm. tsx is a fabulous way to run TypeScript files without a build step. And then we pass in the path to our file.

    And just like that we should be able to run claude and have it communicate with our MCP server. Check out the video above for a demonstration.

    Why Is This Cool?

    This is awesome because it allows us to connect arbitrary TypeScript functions to Claude Code without needing to set up a build step or a server.

    This is a really powerful way to customize your own Claude Code instance, or other MCP hosts like Cursor and Windsurf.

    As more desktop apps start integrating MCPs, this is going to be a really powerful way to extend them with custom capabilities.

    Share