Fix MCP cancellation, mirror run state, and battle boundaries
This commit is contained in:
+28
-6
@@ -78,6 +78,18 @@ class Server:
|
|||||||
if not isinstance(msg, dict):
|
if not isinstance(msg, dict):
|
||||||
return _error(None, -32600, 'request must be a JSON object')
|
return _error(None, -32600, 'request must be a JSON object')
|
||||||
method, mid = msg.get('method'), msg.get('id')
|
method, mid = msg.get('method'), msg.get('id')
|
||||||
|
valid_id = (mid is None or isinstance(mid, str) or
|
||||||
|
isinstance(mid, (int, float)) and not isinstance(mid, bool) and
|
||||||
|
(not isinstance(mid, float) or math.isfinite(mid)))
|
||||||
|
if msg.get('jsonrpc') != '2.0' or not isinstance(method, str) or not method or not valid_id:
|
||||||
|
return _error(mid if valid_id else None, -32600, 'invalid JSON-RPC request')
|
||||||
|
notification = 'id' not in msg
|
||||||
|
params = msg.get('params', {})
|
||||||
|
if not isinstance(params, dict):
|
||||||
|
return None if notification else _error(mid, -32602, 'params must be an object')
|
||||||
|
# Notifications never produce responses or trigger request-only tools.
|
||||||
|
if notification:
|
||||||
|
return None
|
||||||
if method == 'initialize':
|
if method == 'initialize':
|
||||||
return _ok(mid, {'protocolVersion': '2024-11-05',
|
return _ok(mid, {'protocolVersion': '2024-11-05',
|
||||||
'capabilities': {'tools': {'listChanged': False}},
|
'capabilities': {'tools': {'listChanged': False}},
|
||||||
@@ -87,17 +99,17 @@ class Server:
|
|||||||
if method == 'tools/list':
|
if method == 'tools/list':
|
||||||
return _ok(mid, {'tools': TOOLS})
|
return _ok(mid, {'tools': TOOLS})
|
||||||
if method == 'tools/call':
|
if method == 'tools/call':
|
||||||
params = msg.get('params') or {}
|
name, arguments = params.get('name'), params.get('arguments', {})
|
||||||
result = self.call_tool(params.get('name'), params.get('arguments') or {})
|
if not isinstance(name, str) or not name.strip() or not isinstance(arguments, dict):
|
||||||
|
return _error(mid, -32602, 'tools/call requires a name and object arguments')
|
||||||
|
result = self.call_tool(name, arguments)
|
||||||
return _ok(mid, {'content': [{'type': 'text', 'text': json.dumps(result, ensure_ascii=False)}],
|
return _ok(mid, {'content': [{'type': 'text', 'text': json.dumps(result, ensure_ascii=False)}],
|
||||||
'isError': not result.get('ok', False)})
|
'isError': not result.get('ok', False)})
|
||||||
if mid is None:
|
|
||||||
return None # 通知 (notifications/*) 不回
|
|
||||||
return _error(mid, -32601, 'method not found: %s' % method)
|
return _error(mid, -32601, 'method not found: %s' % method)
|
||||||
|
|
||||||
def call_tool(self, name, args):
|
def call_tool(self, name, args):
|
||||||
try:
|
try:
|
||||||
if name in TASK_TOOLS: # 不抢游戏锁: wait_for_text 期间也能查任务/停任务
|
if name in TASK_TOOLS: # 查任务/停任务不等待正在执行的游戏工具
|
||||||
return self._task_tool(name, args)
|
return self._task_tool(name, args)
|
||||||
with self.lock:
|
with self.lock:
|
||||||
return self._call(name, args)
|
return self._call(name, args)
|
||||||
@@ -113,7 +125,14 @@ class Server:
|
|||||||
if task not in TASKS:
|
if task not in TASKS:
|
||||||
raise ValueError('unknown task %r' % task)
|
raise ValueError('unknown task %r' % task)
|
||||||
params = {k: int(a[k]) for k in TASKS[task] if a.get(k) is not None}
|
params = {k: int(a[k]) for k in TASKS[task] if a.get(k) is not None}
|
||||||
|
# Starting must not race a click that has already checked running().
|
||||||
|
# Reject promptly when busy instead of waiting behind a long OCR poll.
|
||||||
|
if not self.lock.acquire(blocking=False):
|
||||||
|
return {'ok': False, 'error': 'MCP 工具正在执行,完成后再启动 AALC 任务'}
|
||||||
|
try:
|
||||||
return self.tasks.start(task, params)
|
return self.tasks.start(task, params)
|
||||||
|
finally:
|
||||||
|
self.lock.release()
|
||||||
if name == 'task_status':
|
if name == 'task_status':
|
||||||
return self.tasks.status(a.get('lines', 30))
|
return self.tasks.status(a.get('lines', 30))
|
||||||
return self.tasks.stop()
|
return self.tasks.stop()
|
||||||
@@ -216,6 +235,9 @@ def parse_line(line):
|
|||||||
if not line:
|
if not line:
|
||||||
return None, None
|
return None, None
|
||||||
try:
|
try:
|
||||||
return json.loads(line), None
|
msg = json.loads(line)
|
||||||
|
if msg is None:
|
||||||
|
return None, _error(None, -32600, 'request must be a JSON object')
|
||||||
|
return msg, None
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
return None, _error(None, -32700, 'parse error: %s' % e)
|
return None, _error(None, -32700, 'parse error: %s' % e)
|
||||||
|
|||||||
Reference in New Issue
Block a user