Simple TypeScript Project Demonstrating the MCP Model Context Protocol
This project demonstrates how to build a minimal MCP (Model Context Protocol) server and client in TypeScript. The server exposes a simple "add" tool (adds two numbers) and a "greet" resource (returns a greeting). The client connects to the server, lists available tools/resources, and calls the "add" tool.
All code is heavily commented for clarity.
1. Project Structure
mcp-demo/
├── package.json
├── tsconfig.json
├── src/
├── server.ts
└── client.ts
2. Setup
3. MCP Server (src/server.ts)
// Import MCP server classes and Zod for schema validation
import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
// Create a new MCP server instance
const server = new McpServer({
name: "Demo MCP Server",
version: "1.0.0"
});
// Define a simple "add" tool that adds two numbers
server.tool(
"add", // Tool name
{ a: z.number(), b: z.number() }, // Input schema using Zod
async ({ a, b }) => ({
content: [
{ type: "text", text: `Sum: ${a + b}` }
]
})
);
// Define a "greet" resource that returns a greeting for a given name
server.resource(
"greet",
new ResourceTemplate("greet://{name}", { list: undefined }),
async (uri, { name }) => ({
contents: [
{ uri: uri.href, text: `Hello, ${name}!` }
]
})
);
// Start the server using stdio transport (reads from stdin, writes to stdout)
const transport = new StdioServerTransport();
await server.connect(transport);
// The server will now listen for MCP client requests via stdio
How it works:
- The server exposes:
- A tool ("add") for computation (like a POST endpoint).
- A resource ("greet") for data (like a GET endpoint).
- It uses stdio for communication, making it easy to test locally[1][5][8].
4. MCP Client (src/client.ts)
// Import MCP client classes
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
async function main() {
// Create an MCP client instance
const client = new Client(
{ name: "Demo MCP Client", version: "1.0.0" },
{ capabilities: { tools: {}, resources: {}, prompts: {} } }
);
// Set up stdio transport to launch the server as a child process
const transport = new StdioClientTransport({
command: "node",
args: ["./build/server.js"] // Path to compiled server
});
// Connect and initialize
await client.connect(transport);
await client.initialize();
// List available tools
const tools = await client.listTools();
console.log("Available tools:", tools.tools);
// List available resources
const resources = await client.listResources();
console.log("Available resources:", resources.resources);
console.log("Available resource templates:", resources.resourceTemplates);
// Call the "add" tool
const addResult = await client.callTool("add", { a: 2, b: 3 });
console.log("Add result:", addResult.content.text);
// Read the "greet" resource for name "Alice"
const greetResult = await client.readResource({ uri: "greet://Alice" });
console.log("Greet result:", greetResult.contents.text);
// Close the connection
await client.close();
}
main().catch(console.error);
How it works:
- The client launches the server as a subprocess and communicates via stdio.
- It lists tools/resources, calls the "add" tool, and reads the "greet" resource[4][1].
5. Build and Run
- Compile TypeScript:
npx tsc
- Run the client:
node build/client.js
The client will automatically launch the server and interact with it.
6. How MCP Works (Summary Table)
| Component | Role | Example in Code |
|---|
| MCP Server | Exposes tools/resources to LLMs/clients | server.ts |
| MCP Client | Connects to server, discovers and uses tools/resources | client.ts |
| Tool | Action endpoint (like POST) | "add" tool |
| Resource | Data endpoint (like GET) | "greet" resource |
| Transport | Communication channel (stdio, HTTP/SSE) | StdioServerTransport, StdioClientTransport |
7. Key Concepts
- MCP separates context/data provision from LLM logic.
- Tools are for actions (side effects, computation).
- Resources are for data (read-only, no side effects).
- Transports allow flexible communication (stdio for local, SSE/HTTP for remote)[1][4][5].
8. References for Further Learning
- [Official TypeScript SDK Quickstart][1]
- [Client Development Guide][4]
- [Beginner Tutorials][5][8]
This project gives you a minimal, fully-commented example of both an MCP server and client in TypeScript, using the official SDK and stdio transport for simplicity. You can expand it by adding more tools/resources or switching to HTTP/SSE transports as needed.
[1][4][5][8]