test-memory-server.sh•5.98 kB
#!/bin/bash
SERVER_URL="http://localhost:3002/mcp"
echo "MCP 内存服务器测试脚本"
echo "======================="
# 检查服务器是否运行
echo "检查服务器状态..."
HEALTH_CHECK=$(curl -s http://localhost:3002/health 2>/dev/null)
if [ $? -eq 0 ]; then
echo "✓ 服务器正在运行"
echo "$HEALTH_CHECK" | jq -r '"服务状态: \(.status), 活跃会话: \(.sessions)"' 2>/dev/null || echo "健康检查响应: $HEALTH_CHECK"
elif ! curl -s $SERVER_URL > /dev/null; then
echo "错误: 服务器未运行,请先启动服务器"
echo "运行: cd src/memory-http && pnpm start"
exit 1
else
echo "✓ 服务器正在运行"
fi
echo -e "\n1. 初始化会话..."
INIT_RESPONSE=$(curl -s -i -X POST $SERVER_URL \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "2024-11-05",
"capabilities": {"tools": {}},
"clientInfo": {"name": "test-client", "version": "1.0.0"}
}
}')
# 从响应头中提取会话ID
SESSION_ID=$(echo "$INIT_RESPONSE" | grep -i "mcp-session-id:" | cut -d' ' -f2 | tr -d '\r\n')
if [ -z "$SESSION_ID" ]; then
echo "错误: 无法获取会话ID"
echo "响应: $INIT_RESPONSE"
exit 1
fi
echo "✓ 会话已初始化,ID: $SESSION_ID"
echo -e "\n2. 获取可用工具列表..."
curl -s -X POST $SERVER_URL \
-H "Content-Type: application/json" \
-H "mcp-session-id: $SESSION_ID" \
-d '{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/list"
}' | jq -r '.result.tools[] | "- \(.name): \(.description)"'
echo -e "\n3. 创建实体..."
CREATE_ENTITIES_RESPONSE=$(curl -s -X POST $SERVER_URL \
-H "Content-Type: application/json" \
-H "mcp-session-id: $SESSION_ID" \
-d '{
"jsonrpc": "2.0",
"id": 3,
"method": "tools/call",
"params": {
"name": "create_entities",
"arguments": {
"entities": [
{
"name": "张三",
"entityType": "人物",
"observations": ["软件工程师", "喜欢编程", "在北京工作"]
},
{
"name": "项目Alpha",
"entityType": "项目",
"observations": ["Web应用", "使用TypeScript", "团队规模3人"]
},
{
"name": "TechCorp",
"entityType": "公司",
"observations": ["科技公司", "总部在深圳", "专注AI开发"]
}
]
}
}
}')
echo "$CREATE_ENTITIES_RESPONSE" | jq '.result.content[0].text | fromjson | length as $count | "✓ 成功创建 \($count) 个实体"'
echo -e "\n4. 创建关系..."
CREATE_RELATIONS_RESPONSE=$(curl -s -X POST $SERVER_URL \
-H "Content-Type: application/json" \
-H "mcp-session-id: $SESSION_ID" \
-d '{
"jsonrpc": "2.0",
"id": 4,
"method": "tools/call",
"params": {
"name": "create_relations",
"arguments": {
"relations": [
{
"from": "张三",
"to": "TechCorp",
"relationType": "在...工作"
},
{
"from": "张三",
"to": "项目Alpha",
"relationType": "负责开发"
}
]
}
}
}')
echo "$CREATE_RELATIONS_RESPONSE" | jq '.result.content[0].text | fromjson | length as $count | "✓ 成功创建 \($count) 个关系"'
echo -e "\n5. 添加观察..."
curl -s -X POST $SERVER_URL \
-H "Content-Type: application/json" \
-H "mcp-session-id: $SESSION_ID" \
-d '{
"jsonrpc": "2.0",
"id": 5,
"method": "tools/call",
"params": {
"name": "add_observations",
"arguments": {
"observations": [
{
"entityName": "张三",
"contents": ["精通React", "有3年工作经验", "毕业于清华大学"]
}
]
}
}
}' | jq '.result.content[0].text | fromjson | .[0] | "✓ 为 \(.entityName) 添加了 \(.addedObservations | length) 个观察"'
echo -e "\n6. 搜索实体..."
echo "搜索关键词: '软件工程师'"
curl -s -X POST $SERVER_URL \
-H "Content-Type: application/json" \
-H "mcp-session-id: $SESSION_ID" \
-d '{
"jsonrpc": "2.0",
"id": 6,
"method": "tools/call",
"params": {
"name": "search_nodes",
"arguments": {
"query": "软件工程师"
}
}
}' | jq -r '.result.content[0].text | fromjson | .entities[] | "- \(.name) (\(.entityType)): \(.observations | join(", "))"'
echo -e "\n7. 获取特定实体..."
echo "获取实体: 张三"
curl -s -X POST $SERVER_URL \
-H "Content-Type: application/json" \
-H "mcp-session-id: $SESSION_ID" \
-d '{
"jsonrpc": "2.0",
"id": 7,
"method": "tools/call",
"params": {
"name": "open_nodes",
"arguments": {
"names": ["张三"]
}
}
}' | jq -r '.result.content[0].text | fromjson | .entities[] | "实体: \(.name)\n类型: \(.entityType)\n观察: \(.observations | join(", "))"'
echo -e "\n8. 查看完整知识图谱..."
FULL_GRAPH=$(curl -s -X POST $SERVER_URL \
-H "Content-Type: application/json" \
-H "mcp-session-id: $SESSION_ID" \
-d '{
"jsonrpc": "2.0",
"id": 8,
"method": "tools/call",
"params": {
"name": "read_graph"
}
}')
echo "知识图谱统计:"
echo "$FULL_GRAPH" | jq -r '.result.content[0].text | fromjson | "- 实体总数: \(.entities | length)\n- 关系总数: \(.relations | length)"'
echo -e "\n实体列表:"
echo "$FULL_GRAPH" | jq -r '.result.content[0].text | fromjson | .entities[] | "- \(.name) (\(.entityType))"'
echo -e "\n关系列表:"
echo "$FULL_GRAPH" | jq -r '.result.content[0].text | fromjson | .relations[] | "- \(.from) \(.relationType) \(.to)"'
echo -e "\n9. 查看存储文件..."
if [ -f "memory.json" ]; then
echo "存储文件内容 (memory.json):"
cat memory.json
else
echo "存储文件不存在于当前目录"
fi
echo -e "\n测试完成!"
echo "================"