FastMCP Demo Server

FastMCP Demo Server

A demonstration server showcasing MCP capabilities with basic tools including addition calculations and weather API integration for fetching city weather data.

Category
访问服务器

README

FastMCP Demo Server

A demonstration server built with FastMCP framework that showcases how to create Model Context Protocol (MCP) servers with custom tools.

What is MCP?

Model Context Protocol (MCP) is an open protocol that enables AI assistants and applications to securely access external data sources and tools. It provides a standardized way for AI models to:

  • Access external APIs and services
  • Interact with databases and file systems
  • Execute custom functions and tools
  • Retrieve context from various sources
  • Perform actions on behalf of users

MCP acts as a bridge between AI applications and external resources, allowing models to extend their capabilities beyond their training data.

Why is MCP Useful?

MCP solves several critical problems in AI application development:

  1. Standardization: Provides a common protocol for AI-to-external-service communication
  2. Security: Enables secure, controlled access to external resources
  3. Extensibility: Allows developers to add custom capabilities without modifying core AI models
  4. Separation of Concerns: Keeps AI logic separate from external service integrations
  5. Real-time Data: Enables AI models to access up-to-date information from external sources
  6. Tool Execution: Allows AI models to perform actions (not just retrieve information)

Architecture of MCP

MCP follows a client-server architecture:

┌─────────────┐         MCP Protocol         ┌─────────────┐
│             │ ◄──────────────────────────► │             │
│ MCP Client  │     (JSON-RPC over stdio)   │ MCP Server  │
│  (AI App)   │                              │  (This App) │
│             │                              │             │
└─────────────┘                              └─────────────┘
                                                      │
                                                      │
                                              ┌───────┴───────┐
                                              │               │
                                         External APIs    Databases
                                         File Systems    Services

Key Components:

  1. MCP Client: The AI application (e.g., Claude Desktop, custom AI app) that makes requests
  2. MCP Server: This application that exposes tools and resources
  3. Transport Layer: JSON-RPC 2.0 protocol over stdio (standard input/output)
  4. Tools: Functions exposed by the server that the client can call
  5. Resources: Data sources that can be accessed by the client
  6. Prompts: Pre-defined prompt templates

Communication Flow:

  1. Client sends a JSON-RPC request to the server
  2. Server processes the request (calls tools, fetches resources)
  3. Server sends a JSON-RPC response back to the client
  4. Client uses the response in its AI context

Lifecycle of MCP

The MCP server lifecycle follows these stages:

1. Initialization

  • Server starts and listens on stdio
  • Server registers available tools, resources, and prompts
  • Server sends initialization message to client

2. Handshake

  • Client and server exchange capabilities
  • Client requests server information (name, version, available tools)
  • Server responds with its capabilities

3. Operation

  • Client sends tool execution requests
  • Server processes requests and executes tools
  • Server returns results to client
  • This cycle repeats for each interaction

4. Shutdown

  • Client sends shutdown signal
  • Server cleans up resources
  • Server terminates gracefully

Example Lifecycle Flow:

Start Server → Initialize FastMCP → Register Tools → Listen on stdio
                                                          │
                                                          ▼
Client Connects → Handshake → Request Tool → Execute Tool → Return Result
                                                          │
                                                          ▼
                                              Continue Listening...

How to Use This Demo Server

Prerequisites

  • Python 3.12 or higher
  • uv package manager (or pip)

Installation

  1. Install dependencies:
uv sync
# or
pip install fastmcp>=2.14.0 requests

Running the Server

Using uv (Recommended)

uv provides a fast and reliable way to manage dependencies and run Python projects:

# Install dependencies (creates virtual environment automatically)
uv sync

# Run the server using uv
uv run python main.py

# Or activate the virtual environment first
source .venv/bin/activate  # On macOS/Linux
# or
.venv\Scripts\activate  # On Windows
python main.py

Using Standard Python

python main.py

The server will start and listen on standard input/output for MCP protocol messages.

Available Tools

This demo server exposes two tools:

  1. add: Simple addition function

    • Parameters: a (int), b (int)
    • Returns: Sum of a and b
    • Example: add(5, 3) returns 8
  2. weather_api: Fetches weather data for a city

    • Parameters: city (str) - Name of the city
    • Returns: Weather data as text
    • Note: Requires a weather API running at http://127.0.0.1:8080

Connecting from an MCP Client

To use this server with an MCP client (like Claude Desktop):

  1. Configure the client to use this server
  2. The client will communicate via stdio using JSON-RPC
  3. The client can call add or weather_api tools as needed

What are the Business or Real Pain Points MCP Server Resolves?

MCP servers address several real-world challenges:

1. Data Freshness

  • Problem: AI models have training cutoffs and can't access real-time data
  • Solution: MCP servers provide access to live APIs, databases, and services

2. Custom Business Logic

  • Problem: Generic AI models don't know your specific business processes
  • Solution: MCP servers can expose domain-specific tools and workflows

3. Security & Access Control

  • Problem: Direct API access from AI models raises security concerns
  • Solution: MCP servers act as a controlled gateway with proper authentication

4. Integration Complexity

  • Problem: Each AI application needs custom integration code
  • Solution: Standardized MCP protocol works across different AI applications

5. Resource Management

  • Problem: AI models need access to files, databases, and services
  • Solution: MCP servers provide unified access to various resources

6. Action Execution

  • Problem: AI models can only generate text, not perform actions
  • Solution: MCP servers execute tools on behalf of the AI model

Can We Build Something Useful?

Absolutely! Here are practical MCP server ideas:

1. Database Query Server

  • Expose SQL queries as tools
  • Allow AI to query databases safely
  • Use case: Business intelligence, data analysis

2. API Integration Server

  • Connect to multiple third-party APIs
  • Provide unified interface for AI
  • Use case: CRM integration, payment processing

3. File Management Server

  • Read/write files securely
  • Search and organize documents
  • Use case: Document management, code editing

4. Workflow Automation Server

  • Execute business processes
  • Trigger actions in other systems
  • Use case: Task automation, notifications

5. Knowledge Base Server

  • Access internal documentation
  • Search company wikis
  • Use case: Internal support, onboarding

6. Development Tools Server

  • Run tests, deploy code
  • Check logs, monitor systems
  • Use case: DevOps automation, debugging

Project Structure

fastmcp-demo-server/
├── main.py           # Main server file with tool definitions
├── pyproject.toml    # Project dependencies and metadata
├── README.md         # This file
└── uv.lock          # Dependency lock file

Code Overview

The server uses FastMCP framework which simplifies MCP server creation:

  • @mcp.tool decorator registers functions as MCP tools
  • Tools automatically get type hints and documentation
  • Server handles JSON-RPC protocol automatically
  • mcp.run() starts the server and listens for requests

Next Steps

  1. Add more tools to extend functionality
  2. Implement error handling and validation
  3. Add logging for debugging
  4. Create resources and prompts (MCP features)
  5. Deploy the server for production use
  6. Integrate with real APIs and services

Resources

How to seyup an mcp server using uv which is a package manager

create a folder -->fastmcp-demo-server uv init . uv add fastmcp

create asimple server run the sever uv run main.py

tio run the mcp server in dev

uv run fastmcp dev main.py

to run the server

uv run fastmcp run main.py

to install server to claude desktop

uv run fastmcp install claude-desktop main.py

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选