使用说明
TIB MCP
A. 模型上下文协议(MCP) 用于学术工具的服务器,使用 FastMCP.MCP是一个开放标准,允许AI助手通过统一的协议与外部工具和数据源进行交互。此服务器公开了任何兼容MCP的客户端都可以发现和调用的学术工具。
先决条件
- Python 3.14+ --该项目包括
.python-version工具兼容性文件 - 紫外线 --用作包管理器、构建系统和脚本运行器
快速开始
# Clone the repository
git clone https://gitlab.com/TIBHannover/orkg/tib-aissistant/tib-mcp.git
cd tib-mcp
# Create your local environment file
cp .env.example .env
# Install all dependencies (including dev tools)
uv sync
# Start the server
uv run python main.py
默认情况下,服务器启动于 可流式传输http 模式,适用于网络或远程访问。对于本地MCP客户端集成(例如Claude Desktop、带有Copilot的VS Code),您可以切换到 标准 模式。
建筑
运行时被分成几个小层:
main.py和server/main.py是启动FastMCP服务器的入口点。server/app.py构建应用程序实例(create_app)并注册工具。server/core/包含基于环境的配置(Settings运输正常化)。server/tools/公开MCP工具并按命名空间挂载它们。server/services/包含工具使用的业务逻辑。server/utils/包含运行时帮助程序(例如特定于传输的run-kwargs)。
配置
所有配置都是通过环境变量完成的。A. .env 项目根目录中的文件在启动时通过以下方式自动加载 python dotenv。参见 .env.example 用于记录模板。
| 变量 | 描述 | 默认值 | 有效值 |
|---|---|---|---|
MCP_SERVER_NAME |
服务器名称 | TIB MCP Server |
任何名字 |
MCP_TRANSPORT |
传输协议 | streamable-http |
stdio, http, sse, streamable-http |
MCP_HOST |
绑定地址(非stdio传输) | 127.0.0.1 |
任何有效的主机/IP |
MCP_PORT |
端口号(非stdio传输) | 8000 |
任何有效端口 |
MCP_LOG_LEVEL |
记录冗长 | INFO |
DEBUG, INFO, WARNING, ERROR, CRITICAL |
运行服务器
stdio模式
专为与AI客户端直接集成而设计。客户端将服务器作为子进程启动,并通过stdin/stdout进行通信:
uv run python main.py
例如,要在MCP客户端中配置此服务器,请将其指向:
{
"mcpServers": {
"tib-mcp": {
"command": "uv",
"args": ["run", "python", "main.py"],
"cwd": "/path/to/tib-mcp"
}
}
}
HTTP模式
对于网络或远程访问,请使用 streamable-http 运输:
# Via .env file
MCP_TRANSPORT=streamable-http
# Or inline
MCP_TRANSPORT=streamable-http uv run python main.py
服务器将在以下时间可用 http://127.0.0.1:8000/mcp.
尝试使用MCP检查器
您可以使用 @modelcontextprotocol/inspector:
# Inspect via stdio (launches this server as a subprocess)
npx @modelcontextprotocol/inspector uv run python main.py
如果您的服务器已在中运行 streamable-http 模式:
npx @modelcontextprotocol/inspector
然后连接到 http://127.0.0.1:8000/mcp 从检查器UI。
码头工人
该项目包括一个生产就绪的Dockerfile,默认为 streamable-http 运输 0.0.0.0:8000:
# Build the image
docker build -t tib-mcp .
# Run the container
docker run -p 8000:8000 tib-mcp
用环境变量覆盖配置:
docker run -e MCP_LOG_LEVEL=DEBUG -p 8000:8000 tib-mcp
运行测试
uv run pytest
测试套件分为四类:
| 类别 | 位置 | 描述 |
|---|---|---|
| 单元 | tests/tools/test_sum.py |
对每个工具进行直接功能级测试 |
| 配置 | tests/test_config.py |
验证环境变量默认值和覆盖 |
| 合同 | tests/test_contract.py |
验证工具元数据、描述和JSON模式 |
| 整合 | tests/test_integration.py |
端到端MCP客户端/服务器通信测试 |
对于详细输出:
uv run pytest -v
项目结构
tib-mcp/
├── main.py # Root entry point for running the MCP server
├── server/
│ ├── __init__.py
│ ├── app.py # App factory (create_app / get_application)
│ ├── main.py # Server module entry point + exported runtime constants
│ ├── core/
│ │ ├── __init__.py
│ │ └── config.py # Environment config + transport normalization
│ ├── services/
│ │ ├── __init__.py
│ │ └── sum.py # Sum service logic
│ ├── tools/
│ │ ├── __init__.py # Tool mounting (register_tools)
│ │ └── sum.py # Sum MCP sub-server and tool definition
│ └── utils/
│ ├── __init__.py
│ └── runtime.py # Runtime helpers (run kwargs per transport)
├── tests/
│ ├── __init__.py
│ ├── conftest.py # Shared fixtures (mcp_app, mcp_client)
│ ├── test_config.py # Configuration behavior tests
│ ├── test_contract.py # Tool metadata/schema contract tests
│ ├── test_integration.py # MCP client/server integration tests
│ └── tools/
│ ├── __init__.py
│ └── test_sum.py # Unit tests for sum behavior
├── pyproject.toml # Dependencies + tool configuration
├── uv.lock # Dependency lock file
├── Dockerfile # Container image definition
├── .dockerignore
├── .env.example # Environment variable template
├── .pre-commit-config.yaml # Pre-commit hooks (Ruff + checks)
├── AGENTS.md # Repository instructions for coding agents
├── CLAUDE.md
├── LICENSE
└── README.md
发展
预提交钩子
该项目使用 拉夫 用于linting和格式化,通过预提交配置:
uv run pre-commit install
这将在每次提交时自动运行,或使用以下命令手动运行:
uv run pre-commit run --all-files
添加新工具
此项目中的工具遵循与以下相同的模式 sum:一个用于逻辑的服务层和一个用于暴露的FastMCP子服务器。
- 创建服务 在
server/services/my_tool.py:
class MyToolService:
def do_something(self, param: str) -> str:
return param.strip().upper()
- 导出服务 在
server/services/__init__.py:
from server.services.my_tool import MyToolService
__all__ = ["SumService", "MyToolService"]
- 创建MCP工具子服务器 在
server/tools/my_tool.py:
from fastmcp import FastMCP
from server.services import MyToolService
server = FastMCP("my_tool")
my_tool_service = MyToolService()
@server.tool()
def do_something(param: str) -> str:
"""Short description of what this tool does.
Args:
param: Description of the parameter.
Returns:
Description of the return value.
"""
return my_tool_service.do_something(param)
类型提示定义了输入模式,文档字符串作为工具描述公开给MCP客户端。
- 安装工具 在
server/tools/__init__.py:
from fastmcp import FastMCP
from server.tools.my_tool import server as my_tool_server
from server.tools.sum import server as sum_server
def register_tools(app: FastMCP) -> None:
app.mount(sum_server, namespace="sum")
app.mount(my_tool_server, namespace="my_tool")
通过命名空间挂载,此工具可被发现为 my_tool_do_something.
- 添加/更新测试:
- 单元测试
tests/tools/test_my_tool.py(直接功能/服务行为)。 - 合同测试
tests/test_contract.py(工具名称、描述、模式、工具计数)。 - 集成测试
tests/test_integration.py(通过MCP客户端调用该工具)。
然后运行:
uv run pytest
uv run pre-commit run --all-files
许可证
该项目根据 MIT许可证©2026 ORKG/TIB团队
