Skip to content

Instantly share code, notes, and snippets.

@jherr
Last active November 20, 2025 14:58
Show Gist options
  • Select an option

  • Save jherr/8eedfc99cd3d80ba4bc23f48ced06c94 to your computer and use it in GitHub Desktop.

Select an option

Save jherr/8eedfc99cd3d80ba4bc23f48ced06c94 to your computer and use it in GitHub Desktop.
MCP walkthrough
server.registerTool(
"get_users",
{
description: "Get a list of users",
inputSchema: {},
},
async ({}) => {
const response = await fetch("https://jsonplaceholder.typicode.com/users");
const users = await response.json();
return {
content: [
{
type: "text",
text: JSON.stringify(users),
},
],
};
}
);

Project Setup

npm init -y
npm install @modelcontextprotocol/sdk zod@3
npm install -D @types/node typescript tsx
mkdir src
touch src/index.ts

package.json changes:

{
  "type": "module",
  "bin": {
    "weather": "./build/index.js"
  },
  "scripts": {
    "build": "tsc && chmod 755 build/index.js"
  },
  "files": ["build"]
}

tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "Node16",
    "moduleResolution": "Node16",
    "outDir": "./build",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

Server Code

src/index.ts:

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";

/*
Never use console.log!!! Use console.error instead.
*/

const server = new McpServer({
  name: "example-mcp-server",
  version: "1.0.0",
});

server.registerTool(
  "say_hello",
  {
    description: "Say hello to a person",
    inputSchema: {
      name: z.string().describe("The name of the person"),
    },
  },
  async ({ name }) => {
    return {
      content: [
        {
          type: "text",
          text: `Hello, ${name}!`,
        },
      ],
    };
  }
);

async function main() {
  const transport = new StdioServerTransport();
  await server.connect(transport);
  console.error("Example MCP Server running on stdio");
}

main().catch((error) => {
  console.error("Fatal error in main():", error);
  process.exit(1);
});

Testing it out:

npx @modelcontextprotocol/inspector@latest

Integrating into Claude desktop:

{
  "mcpServers": {
    "example_server": {
      "command": "node",
      "args": ["/ABSOLUTE/PATH/TO/FOLDER/build/index.js"]
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment