Skip to main content
Glama

Weather MCP Server

by gmailhasan
mcp-http-client.js•3.06 kB
#!/usr/bin/env node import { Client } from "@modelcontextprotocol/sdk/client/index.js"; import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"; // Create MCP client const client = new Client( { name: "weather-http-client", version: "1.0.0", }, { capabilities: {}, } ); const SERVER_URL = "http://localhost:3000/mcp"; async function connectToWeatherServer() { console.log("šŸ”Œ Connecting to Weather MCP Server via HTTP...\n"); // Create HTTP transport const transport = new StreamableHTTPClientTransport(new URL(SERVER_URL)); // Connect the client to the server await client.connect(transport); console.log("āœ… Connected to Weather MCP Server\n"); return transport; } async function listAvailableTools() { console.log("šŸ“‹ Listing available tools...\n"); const response = await client.listTools(); response.tools.forEach((tool) => { console.log(`Tool: ${tool.name}`); console.log(` Description: ${tool.description}`); console.log(` Input Schema:`, JSON.stringify(tool.inputSchema, null, 2)); console.log(); }); return response.tools; } async function callWeatherTool(latitude, longitude, locationName = "Unknown") { console.log( `šŸŒ¤ļø Fetching weather for ${locationName} (${latitude}, ${longitude})...\n` ); const result = await client.callTool({ name: "get_weather", arguments: { latitude, longitude, }, }); // Display the result if (result.content && result.content.length > 0) { result.content.forEach((item) => { if (item.type === "text") { // Only show summary, not full raw data const lines = item.text.split("\n"); const summaryLines = lines.slice(0, 10); // First 10 lines console.log(summaryLines.join("\n")); } }); } if (result.isError) { console.error("\nāŒ Error occurred while fetching weather"); } console.log("\n" + "=".repeat(80) + "\n"); } async function main() { let transport; try { // Connect to the server transport = await connectToWeatherServer(); // List available tools await listAvailableTools(); // Example 1: Get weather for Sammamish, WA await callWeatherTool(47.6162, -122.0356, "Sammamish, WA"); // Example 2: Get weather for New York, NY await callWeatherTool(40.7128, -74.006, "New York, NY"); // Example 3: Get weather for London, UK await callWeatherTool(51.5074, -0.1278, "London, UK"); console.log("✨ All weather queries completed successfully!"); } catch (error) { console.error("āŒ Error:", error.message); console.error(error); } finally { // Close the connection if (transport) { await client.close(); console.log("\nšŸ”Œ Disconnected from Weather MCP Server"); } } } // Run the client main().catch((error) => { console.error("Fatal error:", error); process.exit(1); });

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/gmailhasan/mcpexample'

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