返回提交历史
Modified
g4f/mcp/server.py
+2
-1
Modified
g4f/mcp/tools.py
+80
-0
XFEstudio/gpt4free
Add MarkItDownTool for URL to markdown conversion and update MCPServer to include it
3972bc90
代码差异
2 个文件
+82
-1
@@ -20,7 +20,7 @@ from ..debug import enable_logging
20
20
21
21
enable_logging()
22
22
23
from .tools import WebSearchTool, WebScrapeTool, ImageGenerationTool
23
from .tools import MarkItDownTool, WebSearchTool, WebScrapeTool, ImageGenerationTool
24
24
25
25
26
26
@dataclass
@@ -54,6 +54,7 @@ class MCPServer:
54
54
'web_search': WebSearchTool(),
55
55
'web_scrape': WebScrapeTool(),
56
56
'image_generation': ImageGenerationTool(),
57
'mark_it_down': MarkItDownTool()
57
58
}
58
59
self.server_info = {
59
60
"name": "gpt4free-mcp-server",
@@ -291,3 +291,83 @@ class ImageGenerationTool(MCPTool):
291
291
return {
292
292
"error": f"Image generation failed: {str(e)}"
293
293
}
294
295
class MarkItDownTool(MCPTool):
296
"""MarkItDown tool for converting URLs to markdown format"""
297
298
@property
299
def description(self) -> str:
300
return "Convert a URL to markdown format using MarkItDown. Supports HTTP/HTTPS URLs and returns formatted markdown content."
301
302
@property
303
def input_schema(self) -> Dict[str, Any]:
304
return {
305
"type": "object",
306
"properties": {
307
"url": {
308
"type": "string",
309
"description": "The URL to convert to markdown format (must be HTTP/HTTPS)"
310
},
311
"max_content_length": {
312
"type": "integer",
313
"description": "Maximum content length for processing (default: 10000)",
314
"default": 10000
315
}
316
},
317
"required": ["url"]
318
}
319
320
async def execute(self, arguments: Dict[str, Any]) -> Dict[str, Any]:
321
"""Execute MarkItDown conversion
322
323
Returns:
324
Dict[str, Any]: Markdown content or error message
325
"""
326
try:
327
from ..integration.markitdown import MarkItDown
328
except ImportError as e:
329
return {
330
"error": f"MarkItDown is not installed: {str(e)}"
331
}
332
333
url = arguments.get("url", "")
334
max_content_length = arguments.get("max_content_length", 10000)
335
336
if not url:
337
return {
338
"error": "URL parameter is required"
339
}
340
341
# Validate URL format
342
if not url.startswith(("http://", "https://")):
343
return {
344
"error": "URL must start with http:// or https://"
345
}
346
347
try:
348
# Initialize MarkItDown
349
md = MarkItDown()
350
351
# Convert URL to markdown
352
result = md.convert_url(url)
353
354
if not result:
355
return {
356
"error": "Failed to convert URL to markdown"
357
}
358
359
# Truncate if content exceeds max length
360
if len(result) > max_content_length:
361
result = result[:max_content_length] + "\n\n[Content truncated...]"
362
363
return {
364
"url": url,
365
"markdown_content": result,
366
"content_length": len(result),
367
"truncated": len(result) > max_content_length
368
}
369
370
except Exception as e:
371
return {
372
"error": f"MarkItDown conversion failed: {str(e)}"
373
}