Enhance MCP server and tools: add CORS support, initialize tool_calls, and update WebSearchTool to use CachedSearch

This commit is contained in:
hlohaus 2025-11-01 11:55:42 +01:00
parent a15618a80e
commit 01d194ff4b
3 changed files with 16 additions and 22 deletions

View file

@ -156,6 +156,8 @@ class Api:
messages = kwargs.pop('messages', None) messages = kwargs.pop('messages', None)
action = kwargs.get('action') action = kwargs.get('action')
if action == "continue": if action == "continue":
if "tool_calls" not in kwargs:
kwargs["tool_calls"] = []
kwargs["tool_calls"].append({ kwargs["tool_calls"].append({
"function": { "function": {
"name": "continue_tool" "name": "continue_tool"

View file

@ -14,7 +14,11 @@ import sys
import json import json
import asyncio import asyncio
from typing import Any, Dict, List, Optional, Union from typing import Any, Dict, List, Optional, Union
from dataclasses import dataclass, asdict from dataclasses import dataclass
from ..debug import enable_logging
enable_logging()
from .tools import WebSearchTool, WebScrapeTool, ImageGenerationTool from .tools import WebSearchTool, WebScrapeTool, ImageGenerationTool
@ -232,7 +236,7 @@ class MCPServer:
if response.error is not None: if response.error is not None:
response_dict["error"] = response.error response_dict["error"] = response.error
return web.json_response(response_dict) return web.json_response(response_dict, headers={"access-control-allow-origin": "*"})
except json.JSONDecodeError as e: except json.JSONDecodeError as e:
return web.json_response({ return web.json_response({
@ -262,6 +266,7 @@ class MCPServer:
# Create aiohttp application # Create aiohttp application
app = web.Application() app = web.Application()
app.router.add_options('/mcp', lambda request: web.Response(headers={"access-control-allow-origin": "*", "access-control-allow-methods": "POST, OPTIONS", "access-control-allow-headers": "Content-Type"}))
app.router.add_post('/mcp', handle_mcp_request) app.router.add_post('/mcp', handle_mcp_request)
app.router.add_get('/health', handle_health) app.router.add_get('/health', handle_health)

View file

@ -72,7 +72,7 @@ class WebSearchTool(MCPTool):
Returns: Returns:
Dict[str, Any]: Search results or error message Dict[str, Any]: Search results or error message
""" """
from ..tools.web_search import do_search from ..Provider.search.CachedSearch import CachedSearch
query = arguments.get("query", "") query = arguments.get("query", "")
max_results = arguments.get("max_results", 5) max_results = arguments.get("max_results", 5)
@ -85,26 +85,15 @@ class WebSearchTool(MCPTool):
try: try:
# Perform search - query parameter is used for search execution # Perform search - query parameter is used for search execution
# and prompt parameter holds the content to be searched # and prompt parameter holds the content to be searched
result, sources = await do_search( search_results = await anext(CachedSearch.create_async_generator(
prompt=query, "",
query=query, [],
instructions="" prompt=query
) ))
# Format results
search_results = []
if sources:
for i, source in enumerate(sources[:max_results]):
search_results.append({
"title": source.get("title", ""),
"url": source.get("url", ""),
"snippet": source.get("snippet", "")
})
return { return {
"query": query, "query": query,
"results": search_results, **search_results.get_dict()
"count": len(search_results)
} }
except Exception as e: except Exception as e:
@ -224,8 +213,6 @@ class ImageGenerationTool(MCPTool):
Dict[str, Any]: Generated image data or error message Dict[str, Any]: Generated image data or error message
""" """
from ..client import AsyncClient from ..client import AsyncClient
from ..image import to_data_uri
import base64
prompt = arguments.get("prompt", "") prompt = arguments.get("prompt", "")
model = arguments.get("model", "flux") model = arguments.get("model", "flux")