Initial commit to git.yoin
This commit is contained in:
165
uni-agent/test_servers/a2a_server.py
Normal file
165
uni-agent/test_servers/a2a_server.py
Normal file
@@ -0,0 +1,165 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
A2A 测试服务器 - 简单的 Echo Agent
|
||||
HTTP 服务,提供 Agent Card 和 JSON-RPC 端点
|
||||
"""
|
||||
|
||||
import json
|
||||
import uuid
|
||||
from http.server import HTTPServer, BaseHTTPRequestHandler
|
||||
from urllib.parse import urlparse
|
||||
|
||||
PORT = 8100
|
||||
|
||||
|
||||
class A2AHandler(BaseHTTPRequestHandler):
|
||||
|
||||
tasks = {}
|
||||
|
||||
def log_message(self, format, *args):
|
||||
pass
|
||||
|
||||
def send_json(self, data: dict, status: int = 200):
|
||||
body = json.dumps(data, ensure_ascii=False).encode()
|
||||
self.send_response(status)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Content-Length", len(body))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
def do_GET(self):
|
||||
path = urlparse(self.path).path
|
||||
|
||||
if path == "/.well-known/agent.json":
|
||||
self.send_json({
|
||||
"name": "Test A2A Agent",
|
||||
"description": "A simple echo agent for testing",
|
||||
"url": f"http://localhost:{PORT}/rpc",
|
||||
"version": "1.0.0",
|
||||
"capabilities": {
|
||||
"streaming": False,
|
||||
"pushNotifications": False
|
||||
},
|
||||
"skills": [
|
||||
{
|
||||
"id": "echo",
|
||||
"name": "Echo",
|
||||
"description": "Echo back the message",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"message": {"type": "string"}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "greet",
|
||||
"name": "Greet",
|
||||
"description": "Greet the user",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {"type": "string"}
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"authentication": {
|
||||
"schemes": ["none"]
|
||||
}
|
||||
})
|
||||
else:
|
||||
self.send_json({"error": "Not found"}, 404)
|
||||
|
||||
def do_POST(self):
|
||||
path = urlparse(self.path).path
|
||||
|
||||
content_length = int(self.headers.get("Content-Length", 0))
|
||||
body = self.rfile.read(content_length)
|
||||
|
||||
try:
|
||||
request = json.loads(body)
|
||||
except json.JSONDecodeError:
|
||||
self.send_json({"error": "Invalid JSON"}, 400)
|
||||
return
|
||||
|
||||
if path == "/rpc":
|
||||
self.handle_rpc(request)
|
||||
else:
|
||||
self.send_json({"error": "Not found"}, 404)
|
||||
|
||||
def handle_rpc(self, request: dict):
|
||||
method = request.get("method", "")
|
||||
params = request.get("params", {})
|
||||
req_id = request.get("id", str(uuid.uuid4()))
|
||||
|
||||
if method == "tasks/send":
|
||||
task_id = params.get("id", str(uuid.uuid4()))
|
||||
message = params.get("message", {})
|
||||
content = message.get("parts", [{}])[0].get("text", "") if "parts" in message else message.get("content", "")
|
||||
|
||||
response_text = f"Echo: {content}"
|
||||
|
||||
A2AHandler.tasks[task_id] = {
|
||||
"id": task_id,
|
||||
"status": {"state": "completed"},
|
||||
"history": [
|
||||
message,
|
||||
{"role": "agent", "parts": [{"type": "text", "text": response_text}]}
|
||||
]
|
||||
}
|
||||
|
||||
self.send_json({
|
||||
"jsonrpc": "2.0",
|
||||
"id": req_id,
|
||||
"result": A2AHandler.tasks[task_id]
|
||||
})
|
||||
|
||||
elif method == "tasks/get":
|
||||
task_id = params.get("id", "")
|
||||
if task_id in A2AHandler.tasks:
|
||||
self.send_json({
|
||||
"jsonrpc": "2.0",
|
||||
"id": req_id,
|
||||
"result": A2AHandler.tasks[task_id]
|
||||
})
|
||||
else:
|
||||
self.send_json({
|
||||
"jsonrpc": "2.0",
|
||||
"id": req_id,
|
||||
"error": {"code": -32000, "message": "Task not found"}
|
||||
})
|
||||
|
||||
elif method == "tasks/cancel":
|
||||
task_id = params.get("id", "")
|
||||
if task_id in A2AHandler.tasks:
|
||||
A2AHandler.tasks[task_id]["status"]["state"] = "canceled"
|
||||
self.send_json({
|
||||
"jsonrpc": "2.0",
|
||||
"id": req_id,
|
||||
"result": {"success": True}
|
||||
})
|
||||
else:
|
||||
self.send_json({
|
||||
"jsonrpc": "2.0",
|
||||
"id": req_id,
|
||||
"error": {"code": -32000, "message": "Task not found"}
|
||||
})
|
||||
|
||||
else:
|
||||
self.send_json({
|
||||
"jsonrpc": "2.0",
|
||||
"id": req_id,
|
||||
"error": {"code": -32601, "message": f"Unknown method: {method}"}
|
||||
})
|
||||
|
||||
|
||||
def main():
|
||||
server = HTTPServer(("localhost", PORT), A2AHandler)
|
||||
print(f"A2A Test Server running on http://localhost:{PORT}")
|
||||
print(f"Agent Card: http://localhost:{PORT}/.well-known/agent.json")
|
||||
server.serve_forever()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
146
uni-agent/test_servers/agent_protocol_server.py
Normal file
146
uni-agent/test_servers/agent_protocol_server.py
Normal file
@@ -0,0 +1,146 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Agent Protocol 测试服务器
|
||||
REST API 标准实现
|
||||
"""
|
||||
|
||||
import json
|
||||
import uuid
|
||||
from http.server import HTTPServer, BaseHTTPRequestHandler
|
||||
from urllib.parse import urlparse
|
||||
from datetime import datetime
|
||||
|
||||
PORT = 8102
|
||||
|
||||
|
||||
class APHandler(BaseHTTPRequestHandler):
|
||||
|
||||
tasks = {}
|
||||
|
||||
def log_message(self, format, *args):
|
||||
pass
|
||||
|
||||
def send_json(self, data: dict, status: int = 200):
|
||||
body = json.dumps(data, ensure_ascii=False).encode()
|
||||
self.send_response(status)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Content-Length", len(body))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
def do_GET(self):
|
||||
path = urlparse(self.path).path
|
||||
|
||||
if path == "/" or path == "/ap/v1":
|
||||
self.send_json({
|
||||
"name": "Test Agent Protocol Server",
|
||||
"version": "1.0.0",
|
||||
"protocol_version": "v1"
|
||||
})
|
||||
|
||||
elif path == "/ap/v1/agent/tasks":
|
||||
self.send_json({
|
||||
"tasks": list(APHandler.tasks.values())
|
||||
})
|
||||
|
||||
elif path.startswith("/ap/v1/agent/tasks/"):
|
||||
parts = path.split("/")
|
||||
task_id = parts[5] if len(parts) > 5 else ""
|
||||
|
||||
if "/artifacts" in path:
|
||||
if task_id in APHandler.tasks:
|
||||
self.send_json({
|
||||
"artifacts": APHandler.tasks[task_id].get("artifacts", [])
|
||||
})
|
||||
else:
|
||||
self.send_json({"error": "Task not found"}, 404)
|
||||
|
||||
elif "/steps" in path:
|
||||
if task_id in APHandler.tasks:
|
||||
self.send_json({
|
||||
"steps": APHandler.tasks[task_id].get("steps", [])
|
||||
})
|
||||
else:
|
||||
self.send_json({"error": "Task not found"}, 404)
|
||||
|
||||
else:
|
||||
if task_id in APHandler.tasks:
|
||||
self.send_json(APHandler.tasks[task_id])
|
||||
else:
|
||||
self.send_json({"error": "Task not found"}, 404)
|
||||
|
||||
else:
|
||||
self.send_json({"error": "Not found"}, 404)
|
||||
|
||||
def do_POST(self):
|
||||
path = urlparse(self.path).path
|
||||
|
||||
content_length = int(self.headers.get("Content-Length", 0))
|
||||
body = self.rfile.read(content_length)
|
||||
|
||||
try:
|
||||
request = json.loads(body) if body else {}
|
||||
except json.JSONDecodeError:
|
||||
self.send_json({"error": "Invalid JSON"}, 400)
|
||||
return
|
||||
|
||||
if path == "/ap/v1/agent/tasks":
|
||||
task_id = str(uuid.uuid4())
|
||||
task_input = request.get("input", "")
|
||||
|
||||
APHandler.tasks[task_id] = {
|
||||
"task_id": task_id,
|
||||
"input": task_input,
|
||||
"status": "running",
|
||||
"steps": [],
|
||||
"artifacts": [],
|
||||
"created_at": datetime.now().isoformat()
|
||||
}
|
||||
|
||||
self.send_json(APHandler.tasks[task_id], 201)
|
||||
|
||||
elif path.startswith("/ap/v1/agent/tasks/") and path.endswith("/steps"):
|
||||
parts = path.split("/")
|
||||
task_id = parts[5]
|
||||
|
||||
if task_id not in APHandler.tasks:
|
||||
self.send_json({"error": "Task not found"}, 404)
|
||||
return
|
||||
|
||||
step_input = request.get("input", "")
|
||||
step_id = str(uuid.uuid4())
|
||||
|
||||
step = {
|
||||
"step_id": step_id,
|
||||
"input": step_input,
|
||||
"output": f"Processed: {step_input}" if step_input else "Step executed",
|
||||
"status": "completed",
|
||||
"is_last": True,
|
||||
"created_at": datetime.now().isoformat()
|
||||
}
|
||||
|
||||
APHandler.tasks[task_id]["steps"].append(step)
|
||||
APHandler.tasks[task_id]["status"] = "completed"
|
||||
|
||||
APHandler.tasks[task_id]["artifacts"].append({
|
||||
"artifact_id": str(uuid.uuid4()),
|
||||
"file_name": "output.txt",
|
||||
"relative_path": "/output.txt",
|
||||
"content": step["output"]
|
||||
})
|
||||
|
||||
self.send_json(step, 201)
|
||||
|
||||
else:
|
||||
self.send_json({"error": "Not found"}, 404)
|
||||
|
||||
|
||||
def main():
|
||||
server = HTTPServer(("localhost", PORT), APHandler)
|
||||
print(f"Agent Protocol Test Server running on http://localhost:{PORT}")
|
||||
print(f"API Base: http://localhost:{PORT}/ap/v1")
|
||||
server.serve_forever()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
160
uni-agent/test_servers/aitp_server.py
Normal file
160
uni-agent/test_servers/aitp_server.py
Normal file
@@ -0,0 +1,160 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
AITP 测试服务器 - 模拟交互与交易
|
||||
HTTP 服务,支持 Thread 会话
|
||||
"""
|
||||
|
||||
import json
|
||||
import uuid
|
||||
from http.server import HTTPServer, BaseHTTPRequestHandler
|
||||
from urllib.parse import urlparse
|
||||
from datetime import datetime
|
||||
|
||||
PORT = 8101
|
||||
|
||||
|
||||
class AITPHandler(BaseHTTPRequestHandler):
|
||||
|
||||
threads = {}
|
||||
|
||||
def log_message(self, format, *args):
|
||||
pass
|
||||
|
||||
def send_json(self, data: dict, status: int = 200):
|
||||
body = json.dumps(data, ensure_ascii=False).encode()
|
||||
self.send_response(status)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Content-Length", len(body))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
def do_GET(self):
|
||||
path = urlparse(self.path).path
|
||||
|
||||
if path == "/":
|
||||
self.send_json({
|
||||
"name": "Test AITP Agent",
|
||||
"description": "A simple AITP agent for testing",
|
||||
"version": "1.0.0",
|
||||
"capabilities": ["aitp-01", "aitp-02", "aitp-03"]
|
||||
})
|
||||
|
||||
elif path.startswith("/threads/"):
|
||||
parts = path.split("/")
|
||||
if len(parts) >= 3:
|
||||
thread_id = parts[2]
|
||||
if thread_id in AITPHandler.threads:
|
||||
self.send_json(AITPHandler.threads[thread_id])
|
||||
else:
|
||||
self.send_json({"error": "Thread not found"}, 404)
|
||||
else:
|
||||
self.send_json({"error": "Not found"}, 404)
|
||||
|
||||
def do_POST(self):
|
||||
path = urlparse(self.path).path
|
||||
|
||||
content_length = int(self.headers.get("Content-Length", 0))
|
||||
body = self.rfile.read(content_length)
|
||||
|
||||
try:
|
||||
request = json.loads(body)
|
||||
except json.JSONDecodeError:
|
||||
self.send_json({"error": "Invalid JSON"}, 400)
|
||||
return
|
||||
|
||||
if path == "/threads":
|
||||
thread_id = str(uuid.uuid4())
|
||||
AITPHandler.threads[thread_id] = {
|
||||
"id": thread_id,
|
||||
"status": "open",
|
||||
"messages": [],
|
||||
"created_at": datetime.now().isoformat()
|
||||
}
|
||||
self.send_json({"thread_id": thread_id})
|
||||
|
||||
elif path.startswith("/threads/") and path.endswith("/messages"):
|
||||
parts = path.split("/")
|
||||
thread_id = parts[2]
|
||||
|
||||
if thread_id not in AITPHandler.threads:
|
||||
AITPHandler.threads[thread_id] = {
|
||||
"id": thread_id,
|
||||
"status": "open",
|
||||
"messages": [],
|
||||
"created_at": datetime.now().isoformat()
|
||||
}
|
||||
|
||||
thread = AITPHandler.threads[thread_id]
|
||||
|
||||
if "capability" in request:
|
||||
capability = request.get("capability")
|
||||
|
||||
if capability == "aitp-01":
|
||||
payment_req = request.get("payment_request", {})
|
||||
response = {
|
||||
"role": "agent",
|
||||
"capability": "aitp-01",
|
||||
"payment_response": {
|
||||
"status": "approved",
|
||||
"transaction_id": str(uuid.uuid4()),
|
||||
"amount": payment_req.get("amount"),
|
||||
"currency": payment_req.get("currency", "NEAR"),
|
||||
"timestamp": datetime.now().isoformat()
|
||||
}
|
||||
}
|
||||
|
||||
elif capability == "aitp-02":
|
||||
decision_req = request.get("decision_request", {})
|
||||
response = {
|
||||
"role": "agent",
|
||||
"capability": "aitp-02",
|
||||
"decision_response": {
|
||||
"question": decision_req.get("question"),
|
||||
"selected": decision_req.get("options", ["Yes"])[0] if decision_req.get("options") else "Yes"
|
||||
}
|
||||
}
|
||||
|
||||
elif capability == "aitp-03":
|
||||
data_req = request.get("data_request", {})
|
||||
response = {
|
||||
"role": "agent",
|
||||
"capability": "aitp-03",
|
||||
"data_response": {
|
||||
"schema": data_req.get("schema", {}),
|
||||
"data": {"sample": "test_data", "timestamp": datetime.now().isoformat()}
|
||||
}
|
||||
}
|
||||
|
||||
else:
|
||||
response = {
|
||||
"role": "agent",
|
||||
"error": f"Unknown capability: {capability}"
|
||||
}
|
||||
|
||||
else:
|
||||
message = request.get("message", {})
|
||||
content = message.get("content", "")
|
||||
|
||||
response = {
|
||||
"role": "agent",
|
||||
"content": f"AITP Echo: {content}",
|
||||
"timestamp": datetime.now().isoformat()
|
||||
}
|
||||
|
||||
thread["messages"].append(request)
|
||||
thread["messages"].append(response)
|
||||
|
||||
self.send_json(response)
|
||||
|
||||
else:
|
||||
self.send_json({"error": "Not found"}, 404)
|
||||
|
||||
|
||||
def main():
|
||||
server = HTTPServer(("localhost", PORT), AITPHandler)
|
||||
print(f"AITP Test Server running on http://localhost:{PORT}")
|
||||
server.serve_forever()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
169
uni-agent/test_servers/lmos_server.py
Normal file
169
uni-agent/test_servers/lmos_server.py
Normal file
@@ -0,0 +1,169 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
LMOS 测试服务器 - 模拟企业级 Agent 平台
|
||||
包含注册中心和 Agent 能力调用
|
||||
"""
|
||||
|
||||
import json
|
||||
import uuid
|
||||
from http.server import HTTPServer, BaseHTTPRequestHandler
|
||||
from urllib.parse import urlparse, parse_qs
|
||||
from datetime import datetime
|
||||
|
||||
PORT = 8103
|
||||
|
||||
|
||||
MOCK_AGENTS = [
|
||||
{
|
||||
"id": "calculator",
|
||||
"name": "Calculator Agent",
|
||||
"description": "Performs calculations",
|
||||
"endpoint": f"http://localhost:{PORT}/agents/calculator",
|
||||
"capabilities": [
|
||||
{"id": "add", "description": "Add two numbers"},
|
||||
{"id": "multiply", "description": "Multiply two numbers"}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "greeter",
|
||||
"name": "Greeter Agent",
|
||||
"description": "Greets users",
|
||||
"endpoint": f"http://localhost:{PORT}/agents/greeter",
|
||||
"capabilities": [
|
||||
{"id": "greet", "description": "Greet a user by name"}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
class LMOSHandler(BaseHTTPRequestHandler):
|
||||
|
||||
def log_message(self, format, *args):
|
||||
pass
|
||||
|
||||
def send_json(self, data: dict, status: int = 200):
|
||||
body = json.dumps(data, ensure_ascii=False).encode()
|
||||
self.send_response(status)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Content-Length", len(body))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
def do_GET(self):
|
||||
parsed = urlparse(self.path)
|
||||
path = parsed.path
|
||||
query = parse_qs(parsed.query)
|
||||
|
||||
if path == "/":
|
||||
self.send_json({
|
||||
"name": "Test LMOS Registry",
|
||||
"version": "1.0.0",
|
||||
"agents": len(MOCK_AGENTS)
|
||||
})
|
||||
|
||||
elif path == "/agents":
|
||||
capability = query.get("capability", [None])[0]
|
||||
|
||||
if capability:
|
||||
filtered = [
|
||||
a for a in MOCK_AGENTS
|
||||
if any(c["id"] == capability for c in a["capabilities"])
|
||||
]
|
||||
self.send_json({"agents": filtered})
|
||||
else:
|
||||
self.send_json({"agents": MOCK_AGENTS})
|
||||
|
||||
elif path.startswith("/agents/") and path.endswith("/capabilities"):
|
||||
agent_id = path.split("/")[2]
|
||||
agent = next((a for a in MOCK_AGENTS if a["id"] == agent_id), None)
|
||||
|
||||
if agent:
|
||||
self.send_json({"capabilities": agent["capabilities"]})
|
||||
else:
|
||||
self.send_json({"error": "Agent not found"}, 404)
|
||||
|
||||
else:
|
||||
self.send_json({"error": "Not found"}, 404)
|
||||
|
||||
def do_POST(self):
|
||||
parsed = urlparse(self.path)
|
||||
path = parsed.path
|
||||
|
||||
content_length = int(self.headers.get("Content-Length", 0))
|
||||
body = self.rfile.read(content_length)
|
||||
|
||||
try:
|
||||
request = json.loads(body) if body else {}
|
||||
except json.JSONDecodeError:
|
||||
self.send_json({"error": "Invalid JSON"}, 400)
|
||||
return
|
||||
|
||||
if path.startswith("/agents/") and path.endswith("/invoke"):
|
||||
agent_id = path.split("/")[2]
|
||||
agent = next((a for a in MOCK_AGENTS if a["id"] == agent_id), None)
|
||||
|
||||
if not agent:
|
||||
self.send_json({"error": "Agent not found"}, 404)
|
||||
return
|
||||
|
||||
capability = request.get("capability", "")
|
||||
input_data = request.get("input", {})
|
||||
|
||||
if agent_id == "calculator":
|
||||
if capability == "add":
|
||||
a = input_data.get("a", 0)
|
||||
b = input_data.get("b", 0)
|
||||
result = {"result": a + b}
|
||||
elif capability == "multiply":
|
||||
a = input_data.get("a", 0)
|
||||
b = input_data.get("b", 0)
|
||||
result = {"result": a * b}
|
||||
else:
|
||||
result = {"error": f"Unknown capability: {capability}"}
|
||||
|
||||
elif agent_id == "greeter":
|
||||
if capability == "greet":
|
||||
name = input_data.get("name", "World")
|
||||
result = {"greeting": f"Hello, {name}!"}
|
||||
else:
|
||||
result = {"error": f"Unknown capability: {capability}"}
|
||||
|
||||
else:
|
||||
result = {"error": "Unknown agent"}
|
||||
|
||||
self.send_json({
|
||||
"agent_id": agent_id,
|
||||
"capability": capability,
|
||||
"output": result,
|
||||
"timestamp": datetime.now().isoformat()
|
||||
})
|
||||
|
||||
elif path == "/route":
|
||||
query_text = request.get("query", "")
|
||||
|
||||
if "add" in query_text.lower() or "calculate" in query_text.lower():
|
||||
best_agent = MOCK_AGENTS[0]
|
||||
elif "greet" in query_text.lower() or "hello" in query_text.lower():
|
||||
best_agent = MOCK_AGENTS[1]
|
||||
else:
|
||||
best_agent = MOCK_AGENTS[0]
|
||||
|
||||
self.send_json({
|
||||
"recommended_agent": best_agent,
|
||||
"confidence": 0.85,
|
||||
"alternatives": [a for a in MOCK_AGENTS if a["id"] != best_agent["id"]]
|
||||
})
|
||||
|
||||
else:
|
||||
self.send_json({"error": "Not found"}, 404)
|
||||
|
||||
|
||||
def main():
|
||||
server = HTTPServer(("localhost", PORT), LMOSHandler)
|
||||
print(f"LMOS Test Server running on http://localhost:{PORT}")
|
||||
print(f"Registry: http://localhost:{PORT}/agents")
|
||||
server.serve_forever()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
161
uni-agent/test_servers/mcp_server.py
Normal file
161
uni-agent/test_servers/mcp_server.py
Normal file
@@ -0,0 +1,161 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
MCP 测试服务器 - 简单的 Echo + 计算器
|
||||
通过 stdio 通信
|
||||
"""
|
||||
|
||||
import json
|
||||
import sys
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
def send_response(id: str, result: dict):
|
||||
"""发送 JSON-RPC 响应"""
|
||||
response = {
|
||||
"jsonrpc": "2.0",
|
||||
"id": id,
|
||||
"result": result
|
||||
}
|
||||
msg = json.dumps(response)
|
||||
sys.stdout.write(f"Content-Length: {len(msg)}\r\n\r\n{msg}")
|
||||
sys.stdout.flush()
|
||||
|
||||
|
||||
def send_error(id: str, code: int, message: str):
|
||||
"""发送错误响应"""
|
||||
response = {
|
||||
"jsonrpc": "2.0",
|
||||
"id": id,
|
||||
"error": {"code": code, "message": message}
|
||||
}
|
||||
msg = json.dumps(response)
|
||||
sys.stdout.write(f"Content-Length: {len(msg)}\r\n\r\n{msg}")
|
||||
sys.stdout.flush()
|
||||
|
||||
|
||||
def handle_request(request: dict):
|
||||
"""处理请求"""
|
||||
method = request.get("method", "")
|
||||
params = request.get("params", {})
|
||||
req_id = request.get("id", "0")
|
||||
|
||||
if method == "initialize":
|
||||
send_response(req_id, {
|
||||
"protocolVersion": "2024-11-05",
|
||||
"capabilities": {
|
||||
"tools": {"listChanged": True}
|
||||
},
|
||||
"serverInfo": {
|
||||
"name": "test-mcp-server",
|
||||
"version": "1.0.0"
|
||||
}
|
||||
})
|
||||
|
||||
elif method == "notifications/initialized":
|
||||
pass
|
||||
|
||||
elif method == "tools/list":
|
||||
send_response(req_id, {
|
||||
"tools": [
|
||||
{
|
||||
"name": "echo",
|
||||
"description": "返回输入的消息",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"message": {"type": "string", "description": "要返回的消息"}
|
||||
},
|
||||
"required": ["message"]
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "add",
|
||||
"description": "两数相加",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"a": {"type": "number"},
|
||||
"b": {"type": "number"}
|
||||
},
|
||||
"required": ["a", "b"]
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "get_time",
|
||||
"description": "获取当前时间",
|
||||
"inputSchema": {"type": "object", "properties": {}}
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
elif method == "tools/call":
|
||||
tool_name = params.get("name", "")
|
||||
tool_args = params.get("arguments", {})
|
||||
|
||||
if tool_name == "echo":
|
||||
msg = tool_args.get("message", "")
|
||||
send_response(req_id, {
|
||||
"content": [{"type": "text", "text": f"Echo: {msg}"}]
|
||||
})
|
||||
|
||||
elif tool_name == "add":
|
||||
a = tool_args.get("a", 0)
|
||||
b = tool_args.get("b", 0)
|
||||
send_response(req_id, {
|
||||
"content": [{"type": "text", "text": str(a + b)}]
|
||||
})
|
||||
|
||||
elif tool_name == "get_time":
|
||||
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
send_response(req_id, {
|
||||
"content": [{"type": "text", "text": now}]
|
||||
})
|
||||
|
||||
else:
|
||||
send_error(req_id, -32601, f"Unknown tool: {tool_name}")
|
||||
|
||||
else:
|
||||
send_error(req_id, -32601, f"Unknown method: {method}")
|
||||
|
||||
|
||||
def main():
|
||||
"""主循环 - 读取 stdin,处理请求"""
|
||||
buffer = ""
|
||||
|
||||
while True:
|
||||
try:
|
||||
line = sys.stdin.readline()
|
||||
if not line:
|
||||
break
|
||||
|
||||
buffer += line
|
||||
|
||||
if "Content-Length:" in buffer:
|
||||
parts = buffer.split("\r\n\r\n", 1)
|
||||
if len(parts) == 2:
|
||||
header, body = parts
|
||||
length = int(header.split(":")[1].strip())
|
||||
|
||||
while len(body) < length:
|
||||
body += sys.stdin.read(length - len(body))
|
||||
|
||||
request = json.loads(body[:length])
|
||||
handle_request(request)
|
||||
|
||||
buffer = body[length:]
|
||||
|
||||
elif buffer.strip().startswith("{"):
|
||||
try:
|
||||
request = json.loads(buffer.strip())
|
||||
handle_request(request)
|
||||
buffer = ""
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
|
||||
except Exception as e:
|
||||
sys.stderr.write(f"Error: {e}\n")
|
||||
sys.stderr.flush()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user