MCP游乐场
这是一个用于尝试MCP(模型上下文协议)服务器和Neo4j的个人游乐场。 这 ts-mcp-cypher 目录包含TypeScript实验,而 go-mcp-cypher 包含围棋实验。
这 ts-mcp-cypher 包含所有用于MCP协议和官方SDK实验的实验, 其他包可能具有较少的特征。
建设与发展
设置环境
创建根 .env 具有默认Neo4j配置的文件:
touch .env配置您的环境:
NEO4J_URI="bolt://localhost:7687"
NEO4J_USERNAME="neo4j"
NEO4J_PASSWORD="password"存储库包含多个特定于语言的实现,但有一个根MakeFile,用于在它们之间切换时保持一致性: 请注意,MakeFile仅用作任务运行器;没有进行增量构建。
运行TS-MCP-SERVER
make run-ts检查TS-MCP-SERVER
make inspect-ts清洁TS-MCP-SERVER构建
make clean-ts---GO目标---
构建GO-MCP-SERVER
make build-go运行GO-MCP-SERVER
make run-go检查GO-MCP-SERVER
make inspect-go清洁GO-MCP-SERVER构建
make clean-go尝试一下
要开始尝试它,您可以:
使用检查器
make inspect-ts-快捷方式npx @modelcontextprotocol/inspector our-stdio-server对于HTTP传输: 1. 创建HTTP服务器:make run-ts ARGS="--transport http"1. 将检查器作为单独的流程运行:npx @modelcontextprotocol/inspector
检查STDIO
make inspect-ts您应该看到一个使用mcp检查器打开的浏览器选项卡。
您可以执行一个简单的流程,例如:
- 连接(stdio)
- 列出工具
- 点击阅读密码
- 单击运行工具
服务器日志在检查器左下角的STDERR中可见。 也可以在底部查看历史。
检查HTTP
创建服务器:
make run-ts ARGS="--transport http"单独运行检查器:
npx @modelcontextprotocol/inspector手动启用工具“Admin Cypher”(不重要的流程-仅用于测试会话影响)
忽略此流,此流用于测试在多客户端环境中禁用/启用/删除工具时的行为 使用检查器进行测试可能会产生误导,因为它为选项卡创建了不同的会话,但也为不同的子流程创建了会话 而且工具似乎并不是隐式的会话限制。
要演示特定会话的动态工具,请调用工具 enable-admin-cypher 被暴露为工具。
执行 enable-admin-cypher 将“写入”作为权限传递,以启用 admin-cypher 工具。
您应该看到带有事件的服务器通知 list_changed 上市的。
刷新工具,您将看到 admin-cypher 可用于该会议,同时 enable-admin-cypher 已删除。
当环境变量READ_ONLY存在时,手动处理工具/列表
添加环境变量 READ_ONLY=true 在运行服务器之前,工具/列表将仅返回 read-cypher
READ_ONLY=true make inspect-ts您应该看到调试日志 manually handle list/tools
手动尝试流动
初始化
echo '{ "jsonrpc": "2.0", "id": 1, "method": "initialize", "params": { "protocolVersion": "2024-11-05", "capabilities": { "roots": { "listChanged": true }, "sampling": {}, "elicitation": {} }, "clientInfo": { "name": "bash-test", "title": "Bash test", "version": "1.0.0" } } }' | make run-ts工具/列表
echo '{"jsonrpc": "2.0", "id": 2, "method": "tools/list", "params": {}}' | make run-ts工具/调用
echo '{"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "read-cypher", "arguments": {"cypherQuery": "MATCH (n) RETURN n LIMIT 5"}}}' | make run-ts带有服务器通知的工具/调用
echo '{"jsonrpc": "2.0", "id": 4, "method": "tools/call", "params": { "name": "enable-admin-cypher", "arguments": { "permission": "write" }, "_meta": { "progressToken": 0 } } }' | make run-ts工具/调用已禁用的工具
echo '{"jsonrpc": "2.0", "id": 5, "method": "tools/call", "params": { "name": "admin-cypher", "arguments": { "cypherQuery": "CREATE(n:User) SET n.name = \"MPC-USER\"" }, "_meta": { "progressToken": 2 } } }' | make run-tsjson-rpc客户端
上面为每个工具调用重新创建了一个新流程,以测试有状态的操作,例如 enable-admin-cypher -> admin-cypher 流动, 我制作了一个非常简单的REPL JSON-RPC 2.0客户端,它将JSON-RPC消息作为输入并将其发送到MCP Server子进程。
只需运行:
node json-rpc-client.js node ./ts-mcp-cypher/build/index.js系统将提示您:
Enter JSON-RPC 2.0 message (or "quit" to exit):尝试上述流程,从以下内容开始:
{ "jsonrpc": "2.0", "id": 1, "method": "initialize", "params": { "protocolVersion": "2024-11-05", "capabilities": { "roots": { "listChanged": true }, "sampling": {}, "elicitation": {} }, "clientInfo": { "name": "bash-test", "title": "Bash test", "version": "1.0.0" } } }这 STDERR 被重定向到名为的文件 json-rpc-client-${pid}.debuglog
用VSCode副驾驶进行测试
请注意,只读密码/写密码是公开的,没有详细的描述 并且不返回模式或不公开列表数据库——实际上行为是有限的
运行http服务器:
make run-ts ARGS="--transport http"在VSCode上添加它: mcp.json
{
"servers": {
"mcp-cypher-playground": {
"type": "http",
"url": "http://localhost:3000/mcp/"
}
}
}或作为stdio:
{
"servers": {
"mcp-playground": {
"type": "stdio",
"command": "node",
"args": [
"{{your-folder}}/MCP-playground/ts-mcp-cypher/build/index.js"
],
"env": {
"NEO4J_PASSWORD": "your-password"
}
}
}
}使用NEO4J_TOOLSET进行刀具选择
这 NEO4J_TOOLSET 可以定义环境变量。这演示了如何使用环境变量来启用特定工具。
可用值:
all(默认)-注册所有可用工具:read-cypher,write-cypher,admin-cypher,以及enable-admin-cypherread-cypher-只有只读Cypher查询工具write-cypher-只有写Cypher查询工具admin-cypher-管理员密码工具加上启用工具- 逗号分隔的组合-例如。,
read-cypher,write-cypher(未为演示而修剪,请勿添加额外空格)
示例:
# Only read operations
NEO4J_TOOLSET=read-cypher make inspect-ts
# Read and write operations only
NEO4J_TOOLSET=read-cypher,write-cypher make inspect-ts
# Admin tools only
NEO4J_TOOLSET=admin-cypher make inspect-ts
# All tools (same as default)
NEO4J_TOOLSET=all make inspect-ts注:当 admin-cypher 包括在内 enable-admin-cypher 工具也会自动注册。
实验NEXT
- 测试确认作为服务器发起的启发
- 将实验转换为mark3labs/mcp-go/官方SDK
- genai工具箱实验
