Anthropic Messages

Anthropic Messages

Claude's Messages API supports MCP servers via the tools field on each request. Authenticate with your Xweather key and pass the MCP endpoint in the client configuration.

import Anthropic from "@anthropic-ai/sdk";
 
const client = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
 
const message = await client.messages.create({
  model: "claude-3-5-sonnet-20241022",
  max_tokens: 1024,
  tools: [
    {
      name: "xweather",
      type: "mcp",
      server_url: "https://mcp.api.xweather.com/mcp",
      authorization: process.env.XWEATHER_API_KEY,
      allowed_tools: ["xweather_get_current_weather", "xweather_get_weather_impacts"],
    }
  ],
  messages: [
    {
      role: "user",
      content: "Provide a safety briefing for Austin for the next 6 hours."
    }
  ]
});
 
console.log(message.output_text);

Tips

  • Claude desktop ignores custom headers; use the query parameter fallback if necessary.
  • Keep allowed_tools tight to minimize hallucinated tool calls.
  • Parse the content array in the response to extract tool outputs for logging.