10 min read
Lesson 3 of 6
Building Custom MCP Servers
From the course MCP & AI Tool Integration: Connect AI to Everything
When a pre-built server doesn't exist for your use case, building one is straightforward with the official SDKs.
MCP Server SDK Options
- TypeScript SDK —
@modelcontextprotocol/sdk(most popular) - Python SDK —
mcppackage - Rust, Go, Java — Community SDKs available
Basic TypeScript MCP Server
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
const server = new McpServer({
name: "my-custom-server",
version: "1.0.0",
});
// Define a tool
server.tool(
"get_weather",
"Get current weather for a city",
{
city: { type: "string", description: "City name" },
},
async ({ city }) => {
const response = await fetch(
\`https://api.weather.com/v1/current?city=\${city}\`
);
const data = await response.json();
return {
content: [{ type: "text", text: JSON.stringify(data) }],
};
}
);
// Connect via stdio transport
const transport = new StdioServerTransport();
await server.connect(transport);
Defining Tools
Every tool needs:
- Name — A clear, descriptive function name
- Description — What the tool does (the AI reads this to decide when to use it)
- Parameters — Input schema with types and descriptions
- Handler — The function that executes when called
Defining Resources
Resources expose data for the AI to read:
server.resource(
"config",
"application://config",
async () => ({
contents: [{
uri: "application://config",
text: JSON.stringify(appConfig),
mimeType: "application/json",
}],
})
);
Testing Your Server
- Use the MCP Inspector tool to test servers interactively
- Connect to Claude Desktop and verify tools appear
- Test edge cases: invalid inputs, API failures, timeouts
- Monitor logs for errors during tool execution
Deployment Options
- Local — Run on your machine for personal tools (stdio transport)
- SSE (Server-Sent Events) — Run as a web service for remote access
- Docker — Containerize for consistent deployment
- Cloud functions — Deploy individual tools as serverless functions
Key Takeaways
- •Official SDKs available in TypeScript, Python, and community languages
- •Every tool needs a name, description, parameter schema, and handler function
- •Resources expose data for AI to read; tools expose functions for AI to call
- •Use MCP Inspector to test servers before connecting to AI applications
- •Deploy locally via stdio or remotely via SSE for team-wide access
0 of 6 lessons marked complete
Progress is kept in this browser only.