You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

47 lines
1.5 KiB

"""Shared fixtures for the Agent test suite."""
import sys
import pytest
# A minimal MCP stdio server that also pollutes stdout on purpose: a real server
# must keep working even when something logs to stdout.
FAKE_MCP_SERVER = '''
import json
import sys
def reply(request_id, result):
sys.stdout.write(json.dumps({"jsonrpc": "2.0", "id": request_id, "result": result}) + "\\n")
sys.stdout.flush()
print("这行日志会在 stdout 上,客户端必须忽略它而不是把它当成协议数据")
for line in sys.stdin:
line = line.strip()
if not line:
continue
message = json.loads(line)
method = message.get("method")
if method == "initialize":
reply(message["id"], {"protocolVersion": "2025-03-26"})
elif method == "tools/list":
reply(message["id"], {"tools": [{"name": "echo", "description": "回显文本", "inputSchema": {"type": "object", "properties": {"text": {"type": "string"}}}}]})
elif method == "tools/call":
arguments = message.get("params", {}).get("arguments") or {}
reply(message["id"], {"content": [{"type": "text", "text": "回显:" + str(arguments.get("text", ""))}]})
'''
@pytest.fixture
def mcp_server_script(tmp_path):
"""Return the path to a runnable fake MCP stdio server."""
script = tmp_path / "fake_mcp_server.py"
script.write_text(FAKE_MCP_SERVER, encoding="utf-8")
return str(script)
@pytest.fixture
def python_executable():
return sys.executable