url
Retrieve website content by fetching any URL. Provides the page's raw text for analysis or processing.
Instructions
Fetches a website and returns its content
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes | URL to fetch |
Implementation Reference
- mcp_tool/tools/url_tool.py:1-58 (handler)UrlTool class with execute() method that fetches a URL via HTTP GET and returns the content. This is the core handler for the 'url' tool.
import httpx import mcp.types as types from . import BaseTool, ToolRegistry @ToolRegistry.register class UrlTool(BaseTool): """URL获取工具,用于获取网站内容""" name = "url" description = "Fetches a website and returns its content" input_schema = { "type": "object", "required": ["url"], "properties": { "url": { "type": "string", "description": "URL to fetch", } }, } async def execute(self, arguments: dict) -> list[types.TextContent | types.ImageContent | types.EmbeddedResource]: """获取网站内容""" if "url" not in arguments: return [types.TextContent( type="text", text="Error: Missing required argument 'url'" )] url = arguments["url"] headers = { "User-Agent": "MCP Test Server (github.com/modelcontextprotocol/python-sdk)" } try: timeout = httpx.Timeout(10.0, connect=5.0) async with httpx.AsyncClient( follow_redirects=True, headers=headers, timeout=timeout ) as client: response = await client.get(url) response.raise_for_status() return [types.TextContent(type="text", text=response.text)] except httpx.TimeoutException: return [types.TextContent( type="text", text="Error: Request timed out while trying to fetch the website." )] except httpx.HTTPStatusError as e: return [types.TextContent( type="text", text=(f"Error: HTTP {e.response.status_code} " "error while fetching the website.") )] except Exception as e: return [types.TextContent( type="text", text=f"Error: Failed to fetch website: {str(e)}" )] - mcp_tool/tools/url_tool.py:10-19 (schema)Input schema for the 'url' tool, requiring a 'url' string property.
input_schema = { "type": "object", "required": ["url"], "properties": { "url": { "type": "string", "description": "URL to fetch", } }, } - mcp_tool/tools/url_tool.py:5-5 (registration)The @ToolRegistry.register decorator registers the UrlTool class under the name 'url'.
@ToolRegistry.register - mcp_tool/tools/__init__.py:42-67 (registration)ToolRegistry class that manages tool registration via the @register decorator.
class ToolRegistry: """工具注册器,用于管理所有可用工具""" _tools: Dict[str, Type[BaseTool]] = {} @classmethod def register(cls, tool_class: Type[BaseTool]) -> Type[BaseTool]: """注册工具""" cls._tools[tool_class.name] = tool_class return tool_class @classmethod def get_tool(cls, name: str) -> Type[BaseTool]: """获取工具类""" if name not in cls._tools: raise ValueError(f"Unknown tool: {name}") return cls._tools[name] @classmethod def list_tools(cls) -> List[types.Tool]: """列出所有可用工具""" return [tool_class.get_tool_definition() for tool_class in cls._tools.values()] @classmethod def has_tool(cls, name: str) -> bool: """检查工具是否存在""" return name in cls._tools - mcp_tool/server.py:28-53 (helper)call_tool handler that dispatches tool calls by name to the appropriate tool instance (including 'url').
@app.call_tool() async def fetch_tool( # type: ignore[unused-function] name: str, arguments: dict ) -> list[types.TextContent | types.ImageContent | types.EmbeddedResource]: if ToolRegistry.has_tool(name): tool_instance = tool_instances.get(name) if tool_instance: try: return await tool_instance.execute(arguments) except Exception as e: import traceback error_details = traceback.format_exc() return [types.TextContent( type="text", text=f"Error executing tool {name}: {str(e)}\n{error_details}" )] else: return [types.TextContent( type="text", text=f"Error: Tool instance for {name} not found" )] else: return [types.TextContent( type="text", text=f"Error: Unknown tool: {name}" )]