diff --git a/g4f/gui/server/api.py b/g4f/gui/server/api.py index 89eafa45..714404f1 100644 --- a/g4f/gui/server/api.py +++ b/g4f/gui/server/api.py @@ -156,6 +156,8 @@ class Api: messages = kwargs.pop('messages', None) action = kwargs.get('action') if action == "continue": + if "tool_calls" not in kwargs: + kwargs["tool_calls"] = [] kwargs["tool_calls"].append({ "function": { "name": "continue_tool" diff --git a/g4f/mcp/server.py b/g4f/mcp/server.py index 16f150ba..47b583d4 100644 --- a/g4f/mcp/server.py +++ b/g4f/mcp/server.py @@ -14,7 +14,11 @@ import sys import json import asyncio 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 @@ -232,7 +236,7 @@ class MCPServer: if response.error is not None: 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: return web.json_response({ @@ -262,6 +266,7 @@ class MCPServer: # Create aiohttp 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_get('/health', handle_health) diff --git a/g4f/mcp/tools.py b/g4f/mcp/tools.py index 2ba498f2..1f82d631 100644 --- a/g4f/mcp/tools.py +++ b/g4f/mcp/tools.py @@ -72,7 +72,7 @@ class WebSearchTool(MCPTool): Returns: 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", "") max_results = arguments.get("max_results", 5) @@ -85,26 +85,15 @@ class WebSearchTool(MCPTool): try: # Perform search - query parameter is used for search execution # and prompt parameter holds the content to be searched - result, sources = await do_search( - prompt=query, - query=query, - instructions="" - ) - - # 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", "") - }) + search_results = await anext(CachedSearch.create_async_generator( + "", + [], + prompt=query + )) return { "query": query, - "results": search_results, - "count": len(search_results) + **search_results.get_dict() } except Exception as e: @@ -224,8 +213,6 @@ class ImageGenerationTool(MCPTool): Dict[str, Any]: Generated image data or error message """ from ..client import AsyncClient - from ..image import to_data_uri - import base64 prompt = arguments.get("prompt", "") model = arguments.get("model", "flux")