Spaces:
Sleeping
Sleeping
File size: 14,613 Bytes
5f78432 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
# services/mcp_server.py
"""
Model Context Protocol (MCP) server for MasterLLM.
Exposes CrewAI tools via standardized MCP protocol for external integration.
"""
import json
import os
from typing import Any, Dict, List, Optional
from mcp.server import Server
from mcp.types import Tool, TextContent, ImageContent, EmbeddedResource
from mcp.server.stdio import stdio_server
# Import CrewAI tools
from services.agent_crewai import (
ExtractTextTool,
ExtractTablesTool,
DescribeImagesTool,
SummarizeTextTool,
ClassifyTextTool,
ExtractEntitesTool,
TranslateTextTool,
SignatureVerificationTool,
StampDetectionTool,
get_master_tools,
run_agent,
)
# ========================
# MCP SERVER SETUP
# ========================
class MasterLLMMCPServer:
"""MCP Server for MasterLLM document processing tools."""
def __init__(self, name: str = "masterllm-orchestrator"):
self.server = Server(name)
self.tools = get_master_tools()
self._setup_handlers()
def _setup_handlers(self):
"""Register MCP protocol handlers."""
@self.server.list_tools()
async def list_tools() -> List[Tool]:
"""List all available tools exposed via MCP."""
mcp_tools = []
for tool in self.tools:
# Convert CrewAI tool to MCP tool format
mcp_tool = Tool(
name=tool.name,
description=tool.description,
inputSchema={
"type": "object",
"properties": self._get_tool_schema(tool.name),
"required": self._get_required_fields(tool.name),
}
)
mcp_tools.append(mcp_tool)
return mcp_tools
@self.server.call_tool()
async def call_tool(name: str, arguments: dict) -> List[TextContent]:
"""Execute a tool and return results."""
# Find the matching CrewAI tool
matching_tool = None
for tool in self.tools:
if tool.name == name:
matching_tool = tool
break
if not matching_tool:
return [TextContent(
type="text",
text=json.dumps({"error": f"Tool '{name}' not found"})
)]
try:
# Execute the CrewAI tool
result = matching_tool._run(**arguments)
# Parse result if it's a JSON string
if isinstance(result, str):
try:
result = json.loads(result)
except json.JSONDecodeError:
pass
return [TextContent(
type="text",
text=json.dumps(result, indent=2)
)]
except Exception as e:
return [TextContent(
type="text",
text=json.dumps({
"error": str(e),
"tool": name,
"arguments": arguments
})
)]
@self.server.list_resources()
async def list_resources() -> List[Any]:
"""List available resources (e.g., workflow templates, history)."""
# Can be extended to expose MongoDB records, S3 files, etc.
return [
{
"uri": "workflow://templates",
"name": "Workflow Templates",
"description": "Pre-configured document processing workflows",
"mimeType": "application/json"
},
{
"uri": "workflow://history",
"name": "Execution History",
"description": "Recent workflow execution history",
"mimeType": "application/json"
}
]
@self.server.read_resource()
async def read_resource(uri: str) -> str:
"""Read a specific resource."""
if uri == "workflow://templates":
templates = {
"document_analysis": {
"pipeline": "text-table-summarize",
"description": "Extract text and tables, then summarize"
},
"multilingual_processing": {
"pipeline": "text-translate-summarize",
"description": "Extract, translate, and summarize document"
},
"verification": {
"pipeline": "signature_verification-stamp_detection",
"description": "Verify signatures and detect stamps"
}
}
return json.dumps(templates, indent=2)
elif uri == "workflow://history":
# This could query MongoDB for recent executions
# For now, return placeholder
return json.dumps({
"message": "Connect to MongoDB to view execution history",
"recent_workflows": []
}, indent=2)
return json.dumps({"error": f"Resource not found: {uri}"})
@self.server.list_prompts()
async def list_prompts() -> List[Any]:
"""List available prompt templates."""
return [
{
"name": "analyze_document",
"description": "Comprehensive document analysis workflow",
"arguments": [
{
"name": "file_path",
"description": "Path to the document file",
"required": True
},
{
"name": "analysis_depth",
"description": "Level of analysis: basic, standard, or comprehensive",
"required": False
}
]
},
{
"name": "extract_and_summarize",
"description": "Extract content and generate summary",
"arguments": [
{
"name": "file_path",
"description": "Path to the document file",
"required": True
},
{
"name": "include_tables",
"description": "Whether to include tables in summary",
"required": False
}
]
}
]
@self.server.get_prompt()
async def get_prompt(name: str, arguments: dict) -> Any:
"""Get a specific prompt with filled arguments."""
if name == "analyze_document":
file_path = arguments.get("file_path", "")
depth = arguments.get("analysis_depth", "standard")
if depth == "comprehensive":
instruction = f"Perform comprehensive analysis on {file_path}: extract text, tables, describe images, classify content, extract entities, verify signatures, and detect stamps. Then provide a detailed summary."
elif depth == "basic":
instruction = f"Perform basic analysis on {file_path}: extract text and provide a brief summary."
else: # standard
instruction = f"Analyze {file_path}: extract text and tables, then provide a summary of the content."
return {
"messages": [
{
"role": "user",
"content": {
"type": "text",
"text": instruction
}
}
]
}
elif name == "extract_and_summarize":
file_path = arguments.get("file_path", "")
include_tables = arguments.get("include_tables", "true").lower() == "true"
if include_tables:
instruction = f"Extract text and tables from {file_path}, then create a comprehensive summary including the table data."
else:
instruction = f"Extract text from {file_path} and create a summary."
return {
"messages": [
{
"role": "user",
"content": {
"type": "text",
"text": instruction
}
}
]
}
return {"error": f"Prompt not found: {name}"}
def _get_tool_schema(self, tool_name: str) -> Dict[str, Any]:
"""Get JSON schema for tool parameters."""
base_file_schema = {
"file_path": {
"type": "string",
"description": "Absolute or relative path to the file"
},
"start_page": {
"type": "integer",
"description": "Start page (1-indexed)",
"default": 1
},
"end_page": {
"type": "integer",
"description": "End page (inclusive, 1-indexed)",
"default": 1
}
}
text_or_file_schema = {
"text": {
"type": "string",
"description": "Raw text to process (alternative to file_path)"
},
"file_path": {
"type": "string",
"description": "Path to document file (alternative to text)"
},
"start_page": {
"type": "integer",
"description": "Start page for file processing",
"default": 1
},
"end_page": {
"type": "integer",
"description": "End page for file processing",
"default": 1
}
}
schemas = {
"extract_text": base_file_schema,
"extract_tables": base_file_schema,
"describe_images": base_file_schema,
"summarize_text": text_or_file_schema,
"classify_text": text_or_file_schema,
"extract_entities": text_or_file_schema,
"translate_text": {
**text_or_file_schema,
"target_lang": {
"type": "string",
"description": "Target language code (e.g., 'es', 'fr', 'de') or name (e.g., 'Spanish')"
}
},
"signature_verification": base_file_schema,
"stamp_detection": base_file_schema,
}
return schemas.get(tool_name, {})
def _get_required_fields(self, tool_name: str) -> List[str]:
"""Get required fields for each tool."""
file_based_tools = [
"extract_text",
"extract_tables",
"describe_images",
"signature_verification",
"stamp_detection"
]
if tool_name in file_based_tools:
return ["file_path"]
elif tool_name == "translate_text":
return ["target_lang"]
else:
return [] # text or file_path required, but either is acceptable
async def run(self):
"""Run the MCP server using stdio transport."""
async with stdio_server() as (read_stream, write_stream):
await self.server.run(
read_stream,
write_stream,
self.server.create_initialization_options()
)
# ========================
# FASTAPI INTEGRATION
# ========================
def create_mcp_fastapi_routes(app):
"""
Add MCP SSE (Server-Sent Events) endpoints to FastAPI app.
This allows MCP clients to connect via HTTP instead of stdio.
"""
from mcp.server.sse import SseServerTransport
from fastapi import Request
from fastapi.responses import StreamingResponse
from sse_starlette import EventSourceResponse
mcp_server = MasterLLMMCPServer()
@app.get("/mcp/sse")
async def mcp_sse_endpoint(request: Request):
"""SSE endpoint for MCP protocol."""
from mcp.server.sse import sse_transport
async def event_generator():
async with sse_transport() as (read_stream, write_stream):
await mcp_server.server.run(
read_stream,
write_stream,
mcp_server.server.create_initialization_options()
)
return EventSourceResponse(event_generator())
@app.post("/mcp/message")
async def mcp_post_endpoint(request: Request):
"""POST endpoint for MCP messages (alternative to SSE)."""
data = await request.json()
# Handle MCP JSON-RPC requests
method = data.get("method")
params = data.get("params", {})
if method == "tools/list":
tools = await mcp_server.server._tool_list_handler()
return {"jsonrpc": "2.0", "result": tools, "id": data.get("id")}
elif method == "tools/call":
name = params.get("name")
arguments = params.get("arguments", {})
result = await mcp_server.server._tool_call_handler(name, arguments)
return {"jsonrpc": "2.0", "result": result, "id": data.get("id")}
return {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": data.get("id")}
# ========================
# STANDALONE SERVER
# ========================
async def main():
"""Run MCP server in standalone mode (stdio transport)."""
server = MasterLLMMCPServer()
await server.run()
if __name__ == "__main__":
import asyncio
asyncio.run(main())
|