返回提交历史
Modified
g4f/gui/server/api.py
+2
-0
Modified
g4f/mcp/server.py
+7
-2
Modified
g4f/mcp/tools.py
+7
-20
XFEstudio/gpt4free
Enhance MCP server and tools: add CORS support, initialize tool_calls, and update WebSearchTool to use CachedSearch
01d194ff
代码差异
3 个文件
+16
-22
@@ -156,6 +156,8 @@ class Api:
156
156
messages = kwargs.pop('messages', None)
157
157
action = kwargs.get('action')
158
158
if action == "continue":
159
if "tool_calls" not in kwargs:
160
kwargs["tool_calls"] = []
159
161
kwargs["tool_calls"].append({
160
162
"function": {
161
163
"name": "continue_tool"
@@ -14,7 +14,11 @@ import sys
14
14
import json
15
15
import asyncio
16
16
from typing import Any, Dict, List, Optional, Union
17
from dataclasses import dataclass, asdict
17
from dataclasses import dataclass
18
19
from ..debug import enable_logging
20
21
enable_logging()
18
22
19
23
from .tools import WebSearchTool, WebScrapeTool, ImageGenerationTool
20
24
@@ -232,7 +236,7 @@ class MCPServer:
232
236
if response.error is not None:
233
237
response_dict["error"] = response.error
234
238
235
return web.json_response(response_dict)
239
return web.json_response(response_dict, headers={"access-control-allow-origin": "*"})
236
240
237
241
except json.JSONDecodeError as e:
238
242
return web.json_response({
@@ -262,6 +266,7 @@ class MCPServer:
262
266
263
267
# Create aiohttp application
264
268
app = web.Application()
269
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"}))
265
270
app.router.add_post('/mcp', handle_mcp_request)
266
271
app.router.add_get('/health', handle_health)
267
272
@@ -72,7 +72,7 @@ class WebSearchTool(MCPTool):
72
72
Returns:
73
73
Dict[str, Any]: Search results or error message
74
74
"""
75
from ..tools.web_search import do_search
75
from ..Provider.search.CachedSearch import CachedSearch
76
76
77
77
query = arguments.get("query", "")
78
78
max_results = arguments.get("max_results", 5)
@@ -85,26 +85,15 @@ class WebSearchTool(MCPTool):
85
85
try:
86
86
# Perform search - query parameter is used for search execution
87
87
# and prompt parameter holds the content to be searched
88
result, sources = await do_search(
89
prompt=query,
90
query=query,
91
instructions=""
92
)
93
94
# Format results
95
search_results = []
96
if sources:
97
for i, source in enumerate(sources[:max_results]):
98
search_results.append({
99
"title": source.get("title", ""),
100
"url": source.get("url", ""),
101
"snippet": source.get("snippet", "")
102
})
88
search_results = await anext(CachedSearch.create_async_generator(
89
"",
90
[],
91
prompt=query
92
))
103
93
104
94
return {
105
95
"query": query,
106
"results": search_results,
107
"count": len(search_results)
96
**search_results.get_dict()
108
97
}
109
98
110
99
except Exception as e:
@@ -224,8 +213,6 @@ class ImageGenerationTool(MCPTool):
224
213
Dict[str, Any]: Generated image data or error message
225
214
"""
226
215
from ..client import AsyncClient
227
from ..image import to_data_uri
228
import base64
229
216
230
217
prompt = arguments.get("prompt", "")
231
218
model = arguments.get("model", "flux")