Skip to content

weather-server-typescript: 'fetch is not defined' #106

@nufsty2

Description

@nufsty2

Describe the bug
After following the typescript mcp server tutorial verbatim, claude desktop cannot successfully use the new weather tools. Upon inspecting the logs, this is due to Error making NWS request: ReferenceError: fetch is not defined. Apparently a global fetch isn't available in Node 16. It was introduced in Node 18.

To Reproduce
Steps to reproduce the behavior:

  1. Follow the tutorial verbatim here: https://modelcontextprotocol.io/docs/develop/build-server#typescript
  2. Ask claude desktop about the weather forecast
  3. It will fail and tell you "Failed to retrieve grid point data for coordinates: 38.5816, -121.4944. This location may not be supported by the NWS API (only US locations are supported)."
Image
  1. Ask claude desktop about weather alerts
  2. It will fail and tell you "Failed to retrieve alerts data"
Image

Expected behavior
It is expected that the MCP server we just configured with weather tools will work.

Logs

==> ~/Library/Logs/Claude/mcp-server-weather.log <==
2026-02-18T00:18:30.046Z [weather] [info] Message from client: {"method":"tools/call","params":{"name":"get-alerts","arguments":{"state":"TX"}},"jsonrpc":"2.0","id":3} { metadata: undefined }

==> ~/Library/Logs/Claude/mcp.log <==
2026-02-18T00:18:30.046Z [info] [weather] Message from client: {"method":"tools/call","params":{"name":"get-alerts","arguments":{"state":"TX"}},"jsonrpc":"2.0","id":3}

==> ~/Library/Logs/Claude/mcp-server-weather.log <==
Error making NWS request: ReferenceError: fetch is not defined
    at makeNWSRequest (file://(REDACTED)/weather/build/index.js:13:26)
    at file://(REDACTED)/weather/build/index.js:51:30
    at McpServer.executeToolHandler (file://(REDACTED)/weather/node_modules/@modelcontextprotocol/sdk/dist/esm/server/mcp.js:233:42)
    at file://(REDACTED)/weather/node_modules/@modelcontextprotocol/sdk/dist/esm/server/mcp.js:126:43
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async wrappedHandler (file://(REDACTED)/weather/node_modules/@modelcontextprotocol/sdk/dist/esm/server/index.js:125:32)
2026-02-18T00:18:30.047Z [weather] [info] Message from server: {"jsonrpc":"2.0","id":3,"result":{"content":[{"type":"text","text":"Failed to retrieve alerts data"}]}} { metadata: undefined }

Additional context

  • I have node v22 installed locally.
  • I also tried copying the example code from the repo verbatim.
  • Can be fixed by adding a workaround in index.ts, such as the following:
// Node 16 does not have global fetch; use https when fetch is unavailable
async function nodeFetch(
  url: string,
  headers: Record<string, string>,
): Promise<{ ok: boolean; status: number; json: () => Promise<unknown> }> {
  return new Promise((resolve, reject) => {
    const req = https.get(
      url,
      {
        headers: {
          "User-Agent": USER_AGENT,
          Accept: "application/geo+json",
          ...headers,
        },
      },
      (res) => {
        const chunks: Buffer[] = [];
        res.on("data", (chunk: Buffer) => chunks.push(chunk));
        res.on("end", () => {
          const body = Buffer.concat(chunks).toString("utf8");
          resolve({
            ok: (res.statusCode ?? 0) >= 200 && (res.statusCode ?? 0) < 300,
            status: res.statusCode ?? 0,
            json: async () => JSON.parse(body) as unknown,
          });
        });
      },
    );
    req.on("error", reject);
  });
}

const fetchImpl =
  typeof globalThis.fetch === "function"
    ? globalThis.fetch
    : (url: string, init?: RequestInit) => {
        const headers: Record<string, string> = {};
        if (init?.headers) {
          const h = init.headers as Record<string, string>;
          for (const k of Object.keys(h)) headers[k] = h[k];
        }
        return nodeFetch(url, headers);
      };
  • Another solution would be to manually configure it to use the right node executable:
"mcpServers": {
    "weather": {
      "command": "/(REDACTED)/.nvm/versions/node/v22.16.0/bin/node",
      "args": ["/(REDACTED)/build/index.js"]
    }
  },

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions