Skip to main content
Glama
saksham0712

MCP Complete Implementation Guide

by saksham0712

fetch_url

Retrieve content from any URL using HTTP methods and custom headers to access web data for processing and integration.

Instructions

Fetch content from a URL

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
headersNoHTTP headers to include
methodNoHTTP method (GET, POST, etc.)GET
urlYesThe URL to fetch

Implementation Reference

  • Handler function that fetches URL content using node-fetch and returns structured response including status, headers, and content.
    async fetchUrl(url, method = 'GET', headers = {}) {
      const fetch = require('node-fetch');
      
      try {
        const response = await fetch(url, {
          method,
          headers,
        });
    
        const content = await response.text();
        const responseInfo = {
          status: response.status,
          statusText: response.statusText,
          headers: Object.fromEntries(response.headers),
          content,
        };
    
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(responseInfo, null, 2),
            },
          ],
        };
      } catch (error) {
        throw new Error(`Failed to fetch URL: ${error.message}`);
      }
    }
  • Handler method that uses the requests library to perform HTTP requests and returns a JSON-structured response with status, headers, and content.
    async def fetch_url(
        self, url: str, method: str = "GET", headers: Optional[dict] = None
    ) -> list[types.TextContent]:
        """Fetch content from URL"""
        try:
            response = requests.request(
                method=method,
                url=url,
                headers=headers or {},
                timeout=30,
            )
            
            response_info = {
                "status": response.status_code,
                "statusText": response.reason,
                "headers": dict(response.headers),
                "content": response.text,
            }
            
            return [types.TextContent(type="text", text=json.dumps(response_info, indent=2))]
        except Exception as error:
            raise Exception(f"Failed to fetch URL: {str(error)}")
  • Input schema definition for the fetch_url tool, specifying parameters url (required), method, and headers.
    {
      name: 'fetch_url',
      description: 'Fetch content from a URL',
      inputSchema: {
        type: 'object',
        properties: {
          url: {
            type: 'string',
            description: 'The URL to fetch',
          },
          method: {
            type: 'string',
            description: 'HTTP method (GET, POST, etc.)',
            default: 'GET',
          },
          headers: {
            type: 'object',
            description: 'HTTP headers to include',
          },
        },
        required: ['url'],
      },
    },
  • Input schema definition for the fetch_url tool in the tool registration, matching the JS version.
        name="fetch_url",
        description="Fetch content from a URL",
        inputSchema={
            "type": "object",
            "properties": {
                "url": {
                    "type": "string",
                    "description": "The URL to fetch",
                },
                "method": {
                    "type": "string",
                    "description": "HTTP method (GET, POST, etc.)",
                    "default": "GET",
                },
                "headers": {
                    "type": "object",
                    "description": "HTTP headers to include",
                },
            },
            "required": ["url"],
        },
    ),
  • Inline handler for fetch_url in the ChatGPT proxy server, similar to server.js implementation.
    case 'fetch_url':
      const response = await fetch(args.url, {
        method: args.method || 'GET',
        headers: args.headers || {},
      });
      const responseContent = await response.text();
      return {
        success: true,
        status: response.status,
        statusText: response.statusText,
        headers: Object.fromEntries(response.headers),
        content: responseContent,
      };

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/saksham0712/MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server