Search City
searchCityFind cities matching your search query to locate specific urban areas for further analysis or data retrieval.
Instructions
Find cities matching a query string
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes |
Implementation Reference
- src/server.ts:106-125 (handler)The handler function that implements the core logic of the 'searchCity' tool. It takes a query string, fetches matching cities from the Open-Meteo geocoding API (up to 5 results), formats them as a list, and returns the response in MCP format. Handles errors gracefully.
async ({ query }) => { try { const geoUrl = `https://geocoding-api.open-meteo.com/v1/search?name=${encodeURIComponent(query)}&count=5&language=en&format=json`; const geoRes = await fetch(geoUrl); if (!geoRes.ok) throw new Error("Failed to fetch city search"); const geoData = await geoRes.json() as any; if (!geoData.results || geoData.results.length === 0) return { content: [{ type: "text", text: "No matching cities found." }] }; const matches = geoData.results.map((c: any) => `${c.name}, ${c.country} (${c.latitude},${c.longitude})`).join("\n"); return { content: [{ type: "text", text: `Matching cities:\n${matches}` }] }; } catch (err: any) { return { content: [{ type: "text", text: `Error: ${err.message}` }], isError: true }; } } - src/server.ts:101-104 (schema)The input schema and metadata for the 'searchCity' tool, defining the title, description, and input validation using Zod (query as string).
{ title: "Search City", description: "Find cities matching a query string", inputSchema: { query: z.string() } - src/server.ts:99-126 (registration)The registration of the 'searchCity' tool on the MCP server using server.registerTool, including the tool name, schema, and handler reference.
server.registerTool( "searchCity", { title: "Search City", description: "Find cities matching a query string", inputSchema: { query: z.string() } }, async ({ query }) => { try { const geoUrl = `https://geocoding-api.open-meteo.com/v1/search?name=${encodeURIComponent(query)}&count=5&language=en&format=json`; const geoRes = await fetch(geoUrl); if (!geoRes.ok) throw new Error("Failed to fetch city search"); const geoData = await geoRes.json() as any; if (!geoData.results || geoData.results.length === 0) return { content: [{ type: "text", text: "No matching cities found." }] }; const matches = geoData.results.map((c: any) => `${c.name}, ${c.country} (${c.latitude},${c.longitude})`).join("\n"); return { content: [{ type: "text", text: `Matching cities:\n${matches}` }] }; } catch (err: any) { return { content: [{ type: "text", text: `Error: ${err.message}` }], isError: true }; } } );