mirror of
https://github.com/agentscope-ai/ReMe.git
synced 2026-09-13 23:11:03 +00:00
feat(mcp): add Model Context Protocol service implementation
- Create MCPService class to expose jobs as MCP tools - Implement FastMCP integration with async lifespan management - Add environment variable setup for service information - Implement tool registration for job execution via FunctionTool - Support multiple transport types (sse, stdio) with configurable host/port - Integrate with application lifecycle for proper startup/shutdown
This commit is contained in:
parent
2396e4a854
commit
332ac780b7
1 changed files with 71 additions and 0 deletions
71
reme2/component/service/mcp_service.py
Normal file
71
reme2/component/service/mcp_service.py
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
"""Model Context Protocol (MCP) service implementation."""
|
||||
import json
|
||||
import os
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from fastmcp import FastMCP
|
||||
from fastmcp.server.server import Transport
|
||||
from fastmcp.tools import FunctionTool
|
||||
|
||||
from .base_service import BaseService
|
||||
from ..component_registry import R
|
||||
from ..job import StreamJob
|
||||
from ...constants import REME_DEFAULT_HOST, REME_DEFAULT_PORT, REME_SERVICE_INFO
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ...application import Application
|
||||
from ..job import BaseJob
|
||||
|
||||
|
||||
@R.register("mcp")
|
||||
class MCPService(BaseService):
|
||||
"""Expose jobs as Model Context Protocol (MCP) tools."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
transport: Transport = "sse",
|
||||
host: str = REME_DEFAULT_HOST,
|
||||
port: int = REME_DEFAULT_PORT,
|
||||
**kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self.transport: Transport = transport
|
||||
self.host: str = host
|
||||
self.port: int = port
|
||||
|
||||
def build_service(self, app: "Application") -> None:
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(_: FastMCP):
|
||||
await app.start()
|
||||
service_info = json.dumps({"host": self.host, "port": self.port})
|
||||
os.environ[REME_SERVICE_INFO] = service_info
|
||||
self.logger.info(f"ReMe MCP Service started: {REME_SERVICE_INFO}={service_info}")
|
||||
yield
|
||||
await app.close()
|
||||
|
||||
self.service = FastMCP(name=app.config.app_name, lifespan=lifespan)
|
||||
|
||||
def add_job(self, job: "BaseJob") -> None:
|
||||
if isinstance(job, StreamJob):
|
||||
return
|
||||
|
||||
async def execute_tool(**kwargs):
|
||||
response = await job(**kwargs)
|
||||
return response.answer
|
||||
|
||||
self.service.add_tool(
|
||||
FunctionTool(
|
||||
name=job.name,
|
||||
description=job.description,
|
||||
fn=execute_tool,
|
||||
parameters=job.parameters if job.parameters else None,
|
||||
),
|
||||
)
|
||||
|
||||
def start_service(self, app: "Application") -> None:
|
||||
transport_kwargs = {}
|
||||
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)
|
||||
Loading…
Add table
Reference in a new issue