MCP服务器实现

一个展示如何创建和使用模型上下文协议(MCP)服务器的技术文档,包含服务器实现和客户端应用。

  • by bcperry
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 bcperry/mcp_workshop

使用说明

MCP Workshop

This repository demonstrates how to create and use a Model Context Protocol (MCP) server. The workshop includes both a server implementation and a client application to interact with it.

Portfolio summary

  • Purpose: A compact, hands-on introduction to MCP for engineers who want to understand both sides of the protocol.
  • Scope: A FastMCP tool server and a Semantic Kernel client using Streamable HTTP and automatic function calling.
  • Developer workflow: Reproducible setup with uv or pip, plus MCP Inspector guidance for interactive testing.
  • Use case: Technical enablement, workshops, and rapid prototyping of agent-to-tool integrations.

What is MCP?

The Model Context Protocol (MCP) is an open protocol that standardizes how applications provide context to Large Language Models (LLMs). MCP enables LLMs to securely access tools, data sources, and services through a consistent interface.

Visit modelcontextprotocol.io to learn more about the protocol, its specifications, and available SDKs.

Project Structure

mcp_workshop/
├── server/
│   └── server.py          # MCP server implementation
├── client/
│   └── app.py             # Client application
├── pyproject.toml         # Project configuration
├── requirements.txt       # Python dependencies
└── README.md             # This file

Setup Instructions

Prerequisites

  • Python 3.10 or higher
  • Either UV or pip package manager

Option 1: Setup with UV (Recommended)

UV is a fast Python package installer and resolver.

  1. Install UV (if not already installed):

    # On Windows (PowerShell)
    powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
    
    # On macOS/Linux
    curl -LsSf https://astral.sh/uv/install.sh | sh
    
  2. Install dependencies:

    uv sync
    

    This automatically creates a virtual environment and installs all dependencies. You can also use uv run to execute scripts directly without manually activating the environment.

Option 2: Setup with pip

  1. Create a virtual environment:

    python -m venv .venv
    
  2. Activate the virtual environment:

    # On Windows (PowerShell)
    .venv\Scripts\Activate.ps1
    
    # On macOS/Linux
    source .venv/bin/activate
    
  3. Install dependencies:

    pip install -r requirements.txt
    

Running the MCP Server

Start the MCP server:

# With UV
uv run server/server.py

# Or with activated virtual environment
python server/server.py

Running the Client

In a separate terminal, start the client application:

# With UV
uv run client/app.py

# Or with activated virtual environment
python client/app.py

Using the MCP Inspector

The MCP Inspector is a developer tool for testing and debugging MCP servers. It provides a visual interface to interact with your server and see the available tools, resources, and prompts.

Running the Inspector

npx @modelcontextprotocol/inspector

Then configure it to connect to your MCP server using the appropriate connection settings.

For more information on using the MCP Inspector, visit the official documentation.

Resources

License

MIT License - see LICENSE file for details.