mirror of
https://github.com/agentscope-ai/ReMe.git
synced 2026-11-30 00:47:36 +00:00
* feat(service): expose MCP through HTTP backend Serve JSON/SSE job endpoints and streamable HTTP MCP from one FastAPI application, sharing the same jobs and application lifecycle. Preserve the standalone MCP backend, add configurable MCP HTTP settings, update startup metadata and integration docs, and cover routing, lifecycle, configuration, and compatibility behavior with unit tests. * fix(service): preserve MCP request protections Route the exact MCP path through the complete FastMCP ASGI application so its middleware and state remain active. Reject non-literal MCP paths and validate reserved Job conflicts before tolerant service registration. Add regression coverage for middleware preservation, route syntax, and startup failure. * fix(service): reject encoded MCP paths Reject percent signs in mcp_path so ASGI path decoding cannot turn an accepted configuration into an unreachable route. Cover encoded slash, space, and double-encoded slash inputs.
66 lines
2.2 KiB
Python
66 lines
2.2 KiB
Python
"""MCP service: expose jobs as MCP tools."""
|
|
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
from .base_service import BaseService
|
|
from ..component_registry import R
|
|
from ..job import BaseJob
|
|
from ...constants import REME_DEFAULT_HOST, REME_DEFAULT_PORT
|
|
from .mcp_tools import add_mcp_job
|
|
|
|
if TYPE_CHECKING:
|
|
from fastmcp.server.server import Transport
|
|
from ...application import Application
|
|
|
|
|
|
@R.register("mcp")
|
|
class MCPService(BaseService):
|
|
"""Expose non-stream jobs as MCP tools over stdio, SSE, or streamable-http."""
|
|
|
|
def __init__(
|
|
self,
|
|
transport: "Transport" = "sse",
|
|
host: str = REME_DEFAULT_HOST,
|
|
port: int = REME_DEFAULT_PORT,
|
|
injected_job_kwargs: dict[str, Any] | None = None,
|
|
tool_error_on_failure: bool = False,
|
|
**kwargs,
|
|
):
|
|
super().__init__(**kwargs)
|
|
self.transport: Transport = transport
|
|
self.host: str = host
|
|
self.port: int = port
|
|
self.injected_job_kwargs = dict(injected_job_kwargs or {})
|
|
self.tool_error_on_failure = tool_error_on_failure
|
|
|
|
# ----- BaseService contract ------------------------------------------
|
|
|
|
def build_service(self, app: "Application") -> None:
|
|
"""Construct the FastMCP server."""
|
|
from fastmcp import FastMCP
|
|
|
|
self.service = FastMCP(
|
|
name=app.config.app_name,
|
|
lifespan=self._lifespan(app, self.host, self.port),
|
|
)
|
|
|
|
def add_job(self, job: BaseJob) -> bool:
|
|
"""Register a non-stream job as an MCP tool; StreamJobs are unsupported."""
|
|
return add_mcp_job(
|
|
self.service,
|
|
job,
|
|
injected_job_kwargs=self.injected_job_kwargs,
|
|
tool_error_on_failure=self.tool_error_on_failure,
|
|
)
|
|
|
|
def start_service(self, app: "Application") -> None:
|
|
"""Run the MCP server; bind host/port only for network transports."""
|
|
transport_kwargs: dict = {}
|
|
if self.transport != "stdio":
|
|
transport_kwargs["host"] = self.host
|
|
transport_kwargs["port"] = self.port
|
|
self.service.run(
|
|
transport=self.transport,
|
|
show_banner=False,
|
|
**transport_kwargs,
|
|
)
|