Building Your First MCP Server in TypeScript: Tools, Resources, and Testing

Learn how to build your first Model Context Protocol server in TypeScript: tools, resources, stdio transport, client configuration, and debugging with the MCP Inspector.

What Is the Model Context Protocol?

The Model Context Protocol (MCP) is an open standard that lets AI applications connect to external tools and data through one common interface. Instead of writing a custom integration for every assistant, you write one MCP server and any MCP-compatible client can use it. Anthropic created the protocol in late 2024 and donated it to the Agentic AI Foundation, a Linux Foundation project, in December 2025, so it is now vendor-neutral and widely supported.

The Three Things a Server Can Expose

  • Tools: actions the model can decide to call, such as “create a ticket” or “get the forecast.”
  • Resources: read-only data or context, such as a file or database record.
  • Prompts: reusable, templated workflows a user can trigger.

Project Setup

mkdir weather-mcp && cd weather-mcp
npm init -y
npm install @modelcontextprotocol/sdk zod
npm install -D typescript @types/node

Set "type": "module" in package.json. The SDK’s API evolves, so check its README for the current signatures if anything below differs.

A Minimal Server with One Tool

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

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

server.registerTool(
  "get_forecast",
  {
    title: "Get Forecast",
    description: "Returns a short weather forecast for a city.",
    inputSchema: { city: z.string().min(2).describe("City name, e.g. Lahore") },
  },
  async ({ city }) => {
    // Replace with a real API call
    const forecast = `Forecast for ${city}: sunny, 28°C`;
    return { content: [{ type: "text", text: forecast }] };
  }
);

const transport = new StdioServerTransport();
await server.connect(transport);
console.error("weather MCP server running on stdio");

Critical Rule: Never Log to stdout

With the stdio transport, the protocol messages travel over standard output. A stray console.log corrupts the stream and breaks the connection. Send all diagnostics to stderr with console.error, as shown above.

Adding a Resource

server.registerResource(
  "supported-cities",
  "weather://cities",
  { title: "Supported Cities", mimeType: "application/json" },
  async (uri) => ({
    contents: [{
      uri: uri.href,
      text: JSON.stringify(["Lahore", "Islamabad", "Abbottabad"]),
    }],
  })
);

Connecting It to a Client

Build the project, then register the server in your MCP client’s configuration. Desktop clients typically use a JSON file like this:

{
  "mcpServers": {
    "weather": {
      "command": "node",
      "args": ["/absolute/path/to/weather-mcp/dist/index.js"]
    }
  }
}

Restart the client, and the get_forecast tool should appear. Ask something like “What’s the forecast in Lahore?” and the model will call your tool.

Debugging with the MCP Inspector

npx @modelcontextprotocol/inspector node dist/index.js

The Inspector lets you list tools, call them with test inputs, and view raw protocol messages without needing a full AI client.

Going Remote

Stdio is perfect for local tools. For a shared, hosted server you’d use the Streamable HTTP transport with proper authentication (the spec builds on OAuth 2.1). Start local, prove the tool design, then move to HTTP when multiple users need access.

Design Tips for Good Tools

  • Write precise descriptions. The model decides when to call a tool based on its name and description.
  • Validate inputs strictly with Zod; never trust model-generated arguments.
  • Keep tools narrow. Several focused tools work better than one tool with a dozen modes.
  • Return concise results. Large payloads waste context and slow the model down.

Conclusion

An MCP server is surprisingly small: a few dozen lines expose your data or actions to any compatible assistant. Start with one well-described tool, test with the Inspector, and expand from there.