mirror of
https://github.com/xtekky/gpt4free.git
synced 2025-12-06 02:30:41 -08:00
Enhance MCP server and tools: add CORS support, initialize tool_calls, and update WebSearchTool to use CachedSearch
This commit is contained in:
parent
a15618a80e
commit
01d194ff4b
3 changed files with 16 additions and 22 deletions
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue