Server Development Guide

Comprehensive guide to building MCP servers from scratch.

Guide
1
What is MCP?

Model Context Protocol (MCP) is a standardized way for AI applications to communicate with external data sources and tools. It enables AI models to access real-time information, perform actions, and interact with various services through a consistent interface. MCP provides a common language for AI applications to: - Access databases and APIs - Execute commands and scripts - Retrieve real-time data - Interact with external services - Maintain context across sessions

2
Setting Up Your Environment

Before you can build MCP servers, you'll need to set up your development environment. This includes installing the necessary tools and dependencies.

# Install Python 3.8+
python --version

# Install MCP SDK
pip install mcp-sdk

# Create a new project
mkdir my-mcp-server
cd my-mcp-server
python -m venv venv

# Activate virtual environment
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activate

# Install additional dependencies
pip install asyncio aiohttp
3
Creating Your First Server

Let's create a simple MCP server that responds to basic requests. This example demonstrates the fundamental concepts of MCP server development.

from mcp import Server, StdioServerTransport
from mcp.types import (
    CallToolRequest,
    CallToolResult,
    ListToolsRequest,
    ListToolsResult,
    Tool,
)

class MyFirstServer(Server):
    def __init__(self):
        super().__init__()
        self.register_tool_handler("hello", self.hello)
    
    async def list_tools(self, request: ListToolsRequest) -> ListToolsResult:
        return ListToolsResult(
            tools=[
                Tool(
                    name="hello",
                    description="Say hello to the user",
                    inputSchema={
                        "type": "object",
                        "properties": {
                            "name": {"type": "string", "description": "User's name"}
                        }
                    }
                )
            ]
        )
    
    async def hello(self, request: CallToolRequest) -> CallToolResult:
        name = request.arguments.get("name", "World")
        return CallToolResult(
            content=[
                {
                    "type": "text",
                    "text": f"Hello, {name}! Welcome to MCP."
                }
            ]
        )

if __name__ == "__main__":
    server = MyFirstServer()
    transport = StdioServerTransport()
    
    import asyncio
    asyncio.run(transport.run(server))
4
Testing Your Server

It's important to test your MCP server to ensure it works correctly. Here's how to create basic tests for your server.

import asyncio
from my_first_server import MyFirstServer
from mcp.types import CallToolRequest, ListToolsRequest

async def test_server():
    server = MyFirstServer()
    
    # Test list_tools
    tools_result = await server.list_tools(ListToolsRequest())
    print(f"Available tools: {[tool.name for tool in tools_result.tools]}")
    
    # Test hello tool
    hello_result = await server.hello(
        CallToolRequest(
            name="hello",
            arguments={"name": "Alice"}
        )
    )
    print(f"Response: {hello_result.content}")

if __name__ == "__main__":
    asyncio.run(test_server())
5
Deploying Your Server

Once your server is working locally, you'll want to deploy it for production use. Here are the steps to package and deploy your MCP server.

# Create setup.py for packaging
from setuptools import setup, find_packages

setup(
    name="my-mcp-server",
    version="1.0.0",
    packages=find_packages(),
    install_requires=[
        "mcp-sdk>=1.0.0",
    ],
    entry_points={
        "console_scripts": [
            "my-mcp-server=my_first_server:main",
        ],
    },
)

# Install your server
pip install -e .

# Run the server
my-mcp-server
Quick Navigation
Related Docs

Client Integration Guide

How to integrate MCP servers

API Reference

Complete API documentation