qwen_agent/mcp/directory_tree_wrapper_server.py
2025-10-07 12:25:41 +08:00

61 lines
1.7 KiB
Python

#!/usr/bin/env python3
"""
目录树 MCP包装器服务器
提供安全的目录结构查看功能,限制在项目目录内
"""
import asyncio
import json
import sys
from mcp_wrapper import handle_wrapped_request
async def main():
"""主入口点"""
try:
while True:
# 从stdin读取
line = await asyncio.get_event_loop().run_in_executor(None, sys.stdin.readline)
if not line:
break
line = line.strip()
if not line:
continue
try:
request = json.loads(line)
response = await handle_wrapped_request(request, "directory-tree-wrapper")
# 写入stdout
sys.stdout.write(json.dumps(response) + "\n")
sys.stdout.flush()
except json.JSONDecodeError:
error_response = {
"jsonrpc": "2.0",
"error": {
"code": -32700,
"message": "Parse error"
}
}
sys.stdout.write(json.dumps(error_response) + "\n")
sys.stdout.flush()
except Exception as e:
error_response = {
"jsonrpc": "2.0",
"error": {
"code": -32603,
"message": f"Internal error: {str(e)}"
}
}
sys.stdout.write(json.dumps(error_response) + "\n")
sys.stdout.flush()
except KeyboardInterrupt:
pass
if __name__ == "__main__":
asyncio.run(main())