ReMe/reme/components/service/mcp_tools.py
jinliyl c6049c3fc1
refactor(codex)!: share app-server processes across agent wrappers (#709)
* refactor(codex)!: share app-server and reuse application job tools

* test(codex): use FastMCP input_schema without compatibility aliases

* feat(codex): configure native model catalog and preserve streamed metadata

* fix(codex): default to unattended tools and preserve retry streams

* fix(codex): use an explicit Responses provider for custom endpoints

* fix(codex): store runtime state in workspace .codex

* feat(config): add minimal Studio workspace config with Codex chat agent

- Introduce proactive.yaml defining a workspace with HTTP backend and web enabled
- Add background job for Markdown index update watching daily and digest directories
- Define multiple base and stream backend jobs for chat, config, version, status, health, graph, reindex, and file operations
- Configure Codex app server with API key auth and detailed model catalog including qwen3.8-max
- Set up default agent wrapper using Codex backend with high effort and read-only sandbox
- Establish default tokenizer (regex), keyword index (bm25), file graph and chunker for markdown files
- Configure file storage backend with local implementation and indexing components

* fix(codex): settle cancelled dispatch and isolate TLS callbacks

* test(codex): check cancellation barriers across Python versions
2026-11-04 23:23:13 +08:00

60 lines
2.1 KiB
Python

"""Shared MCP tool registration for services that expose ReMe jobs."""
from typing import Any
from ..job import BaseJob, StreamJob
def add_mcp_job(
server: Any,
job: BaseJob,
*,
injected_job_kwargs: dict[str, Any],
tool_error_on_failure: bool,
) -> bool:
"""Register a non-stream job as an MCP tool on ``server``."""
tool = make_mcp_job_tool(job, injected_job_kwargs=injected_job_kwargs, tool_error_on_failure=tool_error_on_failure)
if tool is None:
return False
server.add_tool(tool)
return True
def make_mcp_job_tool(job: BaseJob, *, injected_job_kwargs: dict[str, Any], tool_error_on_failure: bool) -> Any:
"""Build a tool bound to an existing job and server-owned arguments."""
from fastmcp.exceptions import ToolError
from fastmcp.tools import FunctionTool
if isinstance(job, StreamJob):
return None
async def execute_tool(**kwargs):
conflicts = sorted(injected_job_kwargs.keys() & kwargs.keys())
if conflicts:
names = ", ".join(conflicts)
raise ToolError(
f"{names} injected by the MCP server and cannot be provided by the caller",
)
kwargs.update(injected_job_kwargs)
response = await job(**kwargs)
if tool_error_on_failure and not response.success:
raise ToolError(str(response.answer))
return response.answer
parameters = dict(job.parameters or {})
# MCP requires object inputs even when a job leaves its schema implicit.
parameters.setdefault("type", "object")
injected_names = injected_job_kwargs.keys()
if "properties" in parameters:
parameters["properties"] = {
name: schema for name, schema in parameters["properties"].items() if name not in injected_names
}
if "required" in parameters:
parameters["required"] = [name for name in parameters["required"] if name not in injected_names]
return FunctionTool(
name=job.name,
description=job.description,
fn=execute_tool,
parameters=parameters,
)