Advanced1 min readMay 15, 2025
Building Your First MCP Server in TypeScript
Step-by-step guide to building a Model Context Protocol server from scratch, with tools, resources, and error handling.
Overview
An MCP server acts as the translation layer between an AI runtime and real application capabilities. The minimum useful server usually exposes tools, resources, and a clean error model.
Prerequisites
- TypeScript project setup
- basic understanding of JSON-RPC style request/response flows
- clear boundaries around what the AI is allowed to do
Server skeleton
typescript
8 lines
1export function createServer() {2 return {3 name: 'knowledge-server',4 version: '1.0.0',5 tools: [],6 resources: [],7 }8}Tool design rules
- validate all inputs
- return structured outputs
- make failure states explicit
- keep side effects isolated
Error handling
When a tool fails, the AI should receive enough structure to recover gracefully instead of guessing.
typescript
5 lines
1type ToolError = {2 code: string3 message: string4 retryable: boolean5}Summary
The first production habit to build is strict contracts. A good MCP server is predictable long before it is feature-rich.