How to Build an MCP Server: Complete Tutorial · Alternative Angle
Overview
A step-by-step guide to building your first MCP server using TypeScript or Python. Learn the MCP protocol, tool definitions, and how to connect your server to Claude Desktop or Cursor.
How This Guide Differs
- • Cluster primary: I Built My First MCP Server in 30 Minutes — Here's Exactly How
- • Distinction tokens: an, complete, tutorial
- • This page remains indexable but canonical points to the cluster primary to reduce cannibalization.
Building Your First MCP Server
This guide walks through creating a production-ready MCP server from scratch. You'll build a server that provides code search capabilities, using both the TypeScript and Python SDKs to understand the implementation options.
Prerequisites
Before building an MCP server, ensure you understand basic TypeScript or Python programming, have Node.js 18+ or Python 3.10+ installed, and are familiar with how AI assistants like Claude work with tools.
Project Setup
TypeScript Setup
Initialize a new Node.js project and install the MCP SDK. The SDK provides the abstractions you need to define tools, handle requests, and manage connections.
Create a source file for your server implementation and configure TypeScript for your target environment. The MCP SDK works with both CommonJS and ESM modules.
Python Setup
For Python implementations, install the mcp package from PyPI. Set up a virtual environment for dependency management. The Python SDK provides equivalent functionality to the TypeScript version with Python idioms.
Defining Your First Tool
Tool Schema Design
Each tool in an MCP server has a name, description, and input schema. The input schema uses JSON Schema to define what parameters the tool accepts. Write descriptions as if explaining to an AI colleague—when to use the tool, what inputs it needs, and what output to expect.
For a code search tool, define parameters for the search query, the repository to search, and optional filters like file extensions or line count limits.
Implementation Logic
The tool implementation handles the actual work. For code search, this means querying a code index, filtering results, and formatting the response. Return structured data that AI models can easily incorporate into their responses.
Error Handling
Robust error handling distinguishes production MCP servers from prototypes. Distinguish between recoverable errors (temporary service unavailability) where retry might succeed, and permanent errors (invalid input) where retry won't help. Always return structured error responses rather than letting exceptions propagate.
Testing Your MCP Server
Unit Testing Tool Logic
Write unit tests for your tool implementations independent of the MCP protocol. Mock external dependencies like search indexes or file systems. Test both success and failure paths.
Integration Testing
Test the full MCP server by connecting it to Claude Desktop or Cursor. Verify that tool descriptions appear correctly, inputs are validated properly, and outputs are formatted for AI consumption.
Testing Edge Cases
AI models can generate unexpected inputs. Test with malformed queries, extremely long inputs, special characters, and boundary conditions. Verify that your server handles these gracefully without crashing or returning confusing errors.
Deployment Considerations
Configuration Management
MCP servers typically require some configuration—API keys, database credentials, service endpoints. Use environment variables for sensitive values. Document all configuration options clearly for users.
Scaling
Stateless MCP server design makes horizontal scaling straightforward. Run multiple instances behind a load balancer for high availability. Monitor connection counts and resource usage to plan capacity.
Security
Review the security implications of each tool you expose. Implement authentication on all endpoints. Validate and sanitize all inputs. Log access for auditing. Start with minimal permissions and expand as needed.
Related Guides In This Intent
These pages cover nearby scope with different focus, helping reduce overlap and choose the right guide.
Related MCP Tools
fastmcp
FastMCP is a Python framework for building high-performance MCP servers with minimal boilerplate. It emphasizes speed and simplicity, providing decorators and utilities that let developers create MCP servers from existing Python functions without understanding the full MCP protocol details. Editor's Review: FastMCP is the fastest path from Python function to MCP server. If you have existing Python code that you want to expose as MCP tools, FastMCP lets you do that with minimal additional code. The framework handles the protocol overhead, letting you focus on your tool's logic rather than MCP implementation details. Performance is a key design goal—FastMCP servers have lower latency than naive implementations, which matters for production deployments where tools are called frequently. For Python developers building MCP integrations, FastMCP is the recommended starting point.
mcp-agent
mcp-agent is a Python tool for building effective agents using Model Context Protocol (MCP) and simple workflow patterns.
mcp-use
mcp-use is an opinionated TypeScript framework for building MCP agents, clients, and servers. Built on top of the official @modelcontextprotocol/sdk, it adds structured patterns for authentication, error handling, and composable agent workflows. Editor's Review: mcp-use is the most practical choice for TypeScript developers building production MCP integrations. The framework makes sensible defaults while preserving escape hatches for customization. Notably, it supports ChatGPT Apps, Code Mode, OAuth, Notifications, Sampling, and Observability—covering most real-world MCP use cases in a single library. If you are building an MCP server in TypeScript, starting with mcp-use will save significant time compared to raw SDK usage.
Related Workflows
Related Skills
API Integration Mapping
Use MCP-enabled tooling to handle api integration mapping tasks with repeatable inputs and outputs. Built from observed capabilities in @upstash/context7-mcp, fastmcp, mcp-use.
Workspace Knowledge Routing
Use MCP-enabled tooling to handle workspace knowledge routing tasks with repeatable inputs and outputs. Built from observed capabilities in google_workspace_mcp, @notionhq/notion-mcp-server.
What To Do Next
Move from this guide to a concrete workflow and a matching tool page to apply the concepts.
References
- Model Context Protocol (MCP) — Official Documentation
- MCP Specification & Quick Start
- MCP GitHub Organization
Last updated: March 15, 2026