All StepsStep 3 of 6
3

Building Your First MCP Server

Let's create a basic MCP server that exposes a simple tool to Claude.

Server Setup Create a new file and set up the basic server structure:

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

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

// Define a tool
server.tool(
  "get_weather",
  "Get current weather for a location",
  {
    location: {
      type: "string",
      description: "City name or coordinates",
    },
  },
  async ({ location }) => {
    // Your implementation here
    return {
      content: [
        {
          type: "text",
          text: `Weather for ${location}: 72°F, Sunny`,
        },
      ],
    };
  }
);

// Start the server
const transport = new StdioServerTransport();
await server.connect(transport);