Synapse MCP 服务器
模型上下文协议 (MCP) 服务器,公开 Synapse 实体(数据集、项目、文件夹、文件、表)及其注释。
概述
此服务器提供 RESTful API,用于通过模型上下文协议 (MCP) 访问 Synapse 实体及其注释。它允许您:
- 使用 Synapse 进行身份验证
- 通过 ID 检索实体
- 获取实体注释
- 获取实体子项
- 根据各种条件查询实体
- 查询 Synapse 表
安装
# Clone the repository
git clone https://github.com/SageBionetworks/synapse-mcp.git
cd synapse-mcp
# Create a virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies
pip install -e .
从 PyPI 安装
# Install from PyPI
pip install synapse-mcp
用法
启动服务器
这将在默认端口(9000)上启动 MCP 服务器。
使用 CLI
# Start the server using the CLI
synapse-mcp --host 127.0.0.1 --port 9000 --debug
命令行选项
usage: server.py [-h] [--host HOST] [--port PORT] [--debug]
Run the Synapse MCP server
options:
-h, --help show this help message and exit
--host HOST Host to bind to
--port PORT Port to listen on
--debug Enable debug logging
运行测试
# Run all tests with coverage
./run_tests.sh
# Or run pytest directly
python -m pytest
测试服务器
python examples/client_example.py
API 端点
服务器信息
工具
GET /tools
- 列出可用工具POST /tools/authenticate
- 使用 Synapse 进行身份验证POST /tools/get_entity
- 通过 ID 获取实体POST /tools/get_entity_annotations
- 获取实体的注释POST /tools/get_entity_children
- 获取容器实体的子实体POST /tools/query_entities
- 根据各种条件查询实体POST /tools/query_table
- 查询 Synapse 表
资源
GET /resources
- 列出可用资源GET /resources/entity/{id}
- 通过 ID 获取实体GET /resources/entity/{id}/annotations
- 获取实体注释GET /resources/entity/{id}/children
- 获取实体子项GET /resources/query/entities/{entity_type}
- 按类型查询实体GET /resources/query/entities/parent/{parent_id}
- 通过父 ID 查询实体GET /resources/query/entities/name/{name}
- 按名称查询实体GET /resources/query/table/{id}/{query}
- 使用类似 SQL 的语法查询表
示例
验证
您需要使用真实的 Synapse 凭据进行身份验证才能使用服务器:
import requests
# Authenticate with Synapse
response = requests.post("http://127.0.0.1:9000/tools/authenticate", json={
"email": "your-synapse-email@example.com",
"password": "your-synapse-password"
})
result = response.json()
print(result)
# Alternatively, you can authenticate with an API key
response = requests.post("http://127.0.0.1:9000/tools/authenticate", json={
"api_key": "your-synapse-api-key"
})
获取实体
import requests
# Get an entity by ID
response = requests.get("http://127.0.0.1:9000/resources/entity/syn123456") # Replace with a real Synapse ID
entity = response.json()
print(entity)
获取实体注释
import requests
# Get annotations for an entity
response = requests.get("http://127.0.0.1:9000/resources/entity/syn123456/annotations") # Replace with a real Synapse ID
annotations = response.json()
print(annotations)
查询实体
import requests
# Query for files in a project
response = requests.get("http://127.0.0.1:9000/resources/query/entities/parent/syn123456", params={ # Replace with a real Synapse ID
"entity_type": "file"
})
files = response.json()
print(files)
查询表
import requests
# Query a table
table_id = "syn123456" # Replace with a real Synapse table ID
query = "SELECT * FROM syn123456 LIMIT 10" # Replace with a real Synapse table ID
response = requests.get(f"http://127.0.0.1:9000/resources/query/table/{table_id}/{query}")
table_data = response.json()
print(table_data)
获取 Croissant 格式的数据集
import requests
import json
# Get public datasets in Croissant format
response = requests.get("http://127.0.0.1:9000/resources/croissant/datasets")
croissant_data = response.json()
# Save to file
with open("croissant_metadata.json", "w") as f:
json.dump(croissant_data, f, indent=2)
执照
麻省理工学院