A fully functional starter template for building Model Context Protocol (MCP) servers with TypeScript, supporting both HTTP/SSE and stdio transports for seamless integration with AI applications.
- TypeScript Ready: Full TypeScript support with strict type checking
- SSE Transport: Real-time bidirectional communication using Server-Sent Events
- Zod Validation: Runtime type validation and schema parsing
- RESTful Integration: Example implementation with REST Countries API
- Session Management: Built-in session handling for multiple concurrent connections
- CORS Support: Cross-origin resource sharing configured
- Hot Reload: Development server with
tsxfor instant feedback
src/
βββ index.ts # HTTP server & SSE transport setup
βββ server.ts # MCP server configuration & tool registration
βββ types.ts # TypeScript type definitions
βββ api/ # External API integrations
β βββ fetchRestCountries.ts
βββ parser/ # Zod schema validators
β βββ index.ts
βββ resources/ # MCP resources configurations
β βββ index.ts
βββ tools/ # MCP tool configurations
βββ index.ts
-
Clone and install dependencies:
pnpm install
-
Start the development server:
pnpm start
-
Server endpoints:
- SSE Stream:
GET http://localhost:8000/mcp - Message Endpoint:
POST http://localhost:8000/mcp/messages?sessionId={id}
- SSE Stream:
Create a .env file:
PORT=8000 # Optional, defaults to 8000- Define the tool configuration in
src/tools/index.ts:
export const myNewToolConfig = (): TRegisterTool => ({
title: "My New Tool",
description: "Description of what it does",
inputSchema: {
param1: z.string().describe("Parameter description")
},
outputSchema: {
result: z.any()
}
});- Register the tool in
src/server.ts:
server.registerTool(
"my_new_tool",
myNewToolConfig(),
async ({ param1 }) => {
// Tool implementation
return {
content: [{ type: "text", text: "Result" }],
structuredContent: { result: "data" }
};
}
);The starter includes a complete example that fetches country information:
- Tool Name:
get_country_data - Input:
countryName(string) - API: REST Countries API
- Validation: Zod schema in
src/parser/index.ts
Usage:
// The tool accepts a country name and returns detailed country data
{ "countryName": "Germany" }Core MCP server setup with tool registration and capability configuration.
Handles Server-Sent Events transport layer for real-time communication between client and server.
Handle the client AI application that can't use SSEServerTransport
Type-safe session management for handling multiple concurrent connections.
Example API integration with error handling and response formatting.
This implementation uses SSE (Server-Sent Events) instead of stdio transport:
- β Real-time: Instant bidirectional communication
- β Web-compatible: Works seamlessly with web clients
- β Session-based: Multiple concurrent connections support
- β Error handling: Robust connection management
All schemas are validated using Zod with TypeScript integration:
import z from "zod";
export const mySchema = z.object({
field: z.string().describe("Field description")
});Follow the pattern in src/api/fetchRestCountries.ts:
const fetchMyAPI = async (param: string) => {
try {
const res = await fetch(`https://api.example.com/${param}`);
return await res.json();
} catch (err) {
console.error(`API Error: ${err}`);
return "Error message";
}
};- @modelcontextprotocol/sdk: Core MCP functionality
- zod: Runtime type validation
- tsx: TypeScript execution engine
- dotenv: Environment variable management
This MCP server supports two transport methods:
-
Start the HTTP server:
pnpm start
-
Expose your server with ngrok:
ngrok http 8000
-
Configure ChatGPT:
- Copy the ngrok HTTPS URL (e.g.,
https://abc123.ngrok.io) - In ChatGPT, add your MCP server using the SSE endpoint:
- SSE Stream:
https://abc123.ngrok.io/mcp - Message Endpoint:
https://abc123.ngrok.io/mcp/messages
- SSE Stream:
- Copy the ngrok HTTPS URL (e.g.,
-
Configure Claude Desktop (or other local AI application):
Edit your
claude_desktop_config.jsonfile:{ "mcpServers": { "ts_mcp_starter": { "command": "C:\\Users\\YOUR_USERNAME\\Development\\mcp\\YOUR_MCP_FOLDER\\node_modules\\.bin\\tsx.cmd", "args": ["C:\\Users\\YOUR_USERNAME\\Development\\mcp\\YOUR_MCP_FOLDER\\src\\stdio.ts"], "cwd": "C:\\Users\\YOUR_USERNAME\\Development\\mcp\\YOUR_MCP_FOLDER" } } }Important: Replace
YOUR_USERNAMEwith your actual username and adjust paths as needed.For macOS/Linux:
{ "mcpServers": { "ts_mcp_starter": { "command": "node", "args": ["/absolute/path/to/YOUR_MCP_FOLDER/node_modules/.bin/tsx", "src/stdio.ts"], "cwd": "/absolute/path/to/YOUR_MCP_FOLDER" } } } -
Restart Claude Desktop to load the MCP server
-
Verify connection:
- Check Claude Desktop logs for successful connection
- The server will output to stderr: "MCP Server running on stdio"
| Transport | Use Case | Pros | Cons |
|---|---|---|---|
| HTTP/SSE | ChatGPT, web clients, remote access | Multiple concurrent connections, web-compatible, easy to debug | Requires ngrok for ChatGPT, port management |
| Stdio | Claude Desktop, local AI apps | Direct integration, no network setup, secure | Single connection, local only |
Build and run in production:
# Build the project
npx tsc
# Run the built server
node build/index.jsFor containerized deployment, the server runs on the configured port and accepts HTTP connections for MCP communication.
Ready to build your next MCP server? This starter kit provides everything you need to create robust, type-safe MCP applications with modern TypeScript tooling! π