TypeScript
Create a simple MCP server in TypeScript in 15 minutes
Let’s build your first MCP server in TypeScript! We’ll create a weather server that provides current weather data as a resource and lets Claude fetch forecasts using tools.
This guide uses the OpenWeatherMap API. You’ll need a free API key from OpenWeatherMap to follow along.
Prerequisites
Install Node.js
You’ll need Node.js 18 or higher:
Create a new project
You can use our create-typescript-server tool to bootstrap a new project:
Install dependencies
Set up environment
Create .env
:
Make sure to add your environment file to .gitignore
Create your server
Define types
Create a file src/types.ts
, and add the following:
Add the base code
Replace src/index.ts
with the following:
Add resource handlers
Add this to the setupResourceHandlers
method:
Add tool handlers
Add these handlers to the setupToolHandlers
method:
Build and test
Connect to Claude Desktop
Update Claude config
If you didn’t already connect to Claude Desktop during project setup, add to claude_desktop_config.json
:
Restart Claude
- Quit Claude completely
- Start Claude again
- Look for your weather server in the 🔌 menu
Try it out!
Understanding the code
TypeScript adds type safety to our MCP server, making it more reliable and easier to maintain.
Best practices
Error Handling
When a tool encounters an error, return the error message with isError: true
, so the model can self-correct:
For other handlers, throw an error, so the application can notify the user:
Type Validation
Available transports
While this guide uses stdio to run the MCP server as a local process, MCP supports other transports as well.
Troubleshooting
The following troubleshooting tips are for macOS. Guides for other platforms are coming soon.
Build errors
Runtime errors
Look for detailed error messages in the Claude Desktop logs:
Type errors
Next steps
Architecture overview
Learn more about the MCP architecture
TypeScript SDK
Check out the TypeScript SDK on GitHub
Need help? Ask Claude! Since it has access to the MCP SDK documentation, it can help you debug issues and suggest improvements to your server.