MCP 自动扩展服务器

@ynu/mcp-ynu
0 Stars 338 次浏览 ynu 更新于 2026-08-23

一种动态的MCP服务器实现,可以自动从各自的目录加载工具、资源和提示,从而方便扩展和配置。

该服务暂未提供标准配置,请参考 README 手动接入

服务介绍

MCP-YNU - FastMCP 服务器

使用 FastMCP 动态实现的 MCP 服务器,能够自动从各自的目录中加载工具、资源和提示。

特性

  • tools/resources/prompts/ 目录动态加载模块
  • 自动发现并注册模块
  • 简单配置和可扩展性
  • 类型提示以提高代码清晰度和静态分析
  • 全面的日志记录以便监控服务器活动

最近更新

  • 在整个代码库中添加了类型提示
  • 改进了 MCP 实例处理
  • 添加了日志功能
  • 添加了 MIT 许可证
  • 更新了文档并添加了参考链接

目录结构

mcp-ynu/
├── tools/          # Directory for tool modules
│   ├── __init__.py
│   ├── example.py
├── resources/      # Directory for resource modules
│   ├── __init__.py
│   ├── example.py
├── prompts/        # Directory for prompt modules
│   ├── __init__.py
│   ├── example.py
├── logger.py       # Logger implementation
├── main.py         # Main implementation
├── mcp_server.py   # MCP server implementation
├── README.md       # Project documentation
├── LICENSE         # MIT License
└── pyproject.toml  # Project configuration

使用方法

  1. 在适当的目录中创建模块
  2. 通过 from mcp_server import mcp 导入 mcp
  3. 运行服务器:
python main.py

示例模块

工具模块示例 (tools/example.py)

from mcp_server import mcp
import httpx

@mcp.tool()
def calculate_bmi(weight_kg: float, height_m: float) -> float:
    """Calculate BMI given weight in kg and height in meters"""
    return weight_kg / (height_m**2)


@mcp.tool()
async def fetch_weather(city: str) -> str:
    """Fetch current weather for a city"""
    async with httpx.AsyncClient() as client:
        response = await client.get(f"https://api.weather.com/{city}")
        return response.text

资源模块示例 (resources/example.py)

from mcp_server import mcp

@mcp.resource("config://app")
def get_config() -> str:
    """Static configuration data"""
    return "App configuration here"


@mcp.resource("users://{user_id}/profile")
def get_user_profile(user_id: str) -> str:
    """Dynamic user data"""
    return f"Profile data for user {user_id}"

提示模块示例 (prompts/example.py)

from mcp_server import mcp
from mcp.server.fastmcp.prompts import base

@mcp.prompt()
def review_code(code: str) -> str:
    return f"Please review this code:\n\n{code}"


@mcp.prompt()
def debug_error(error: str) -> list[base.Message]:
    return [
        base.UserMessage("I'm seeing this error:"),
        base.UserMessage(error),
        base.AssistantMessage("I'll help debug that. What have you tried so far?"),
    ]

调试

  1. .env 中更新 MCP_TRANSPORT_TYPE,执行 python main.py 启动 mcp 服务器
  2. 执行 npx @modelcontextprotocol/inspector 打开 inspect
  3. 选择 SSE 传输类型,URL 为 http://localhost:<mcp_server_port>/sse 或者选择 STDIO 传输类型,命令为 python,参数为 /path/to/main.py

依赖

  • Python >= 3.10
  • FastMCP

参考链接

许可证

本项目采用 MIT 许可证 - 详情请参阅 LICENSE 文件。

相关 MCP 服务