google.ts•3.15 kB
import { BaseApiClient } from "../../core/base-api.js";
import { loadConfig } from "../../core/config.js";
import { ToolRegistration } from "../../core/types.js";
class GoogleMapsClient extends BaseApiClient {
private readonly apiKey: string;
constructor(apiKey: string) {
super("https://maps.googleapis.com");
this.apiKey = apiKey;
}
searchPlaces(query: string, location?: string) {
return this.request("/maps/api/place/textsearch/json", {
query: { query, location, key: this.apiKey },
});
}
getDirections(origin: string, destination: string) {
return this.request("/maps/api/directions/json", {
query: { origin, destination, key: this.apiKey },
});
}
geocode(address: string) {
return this.request("/maps/api/geocode/json", {
query: { address, key: this.apiKey },
});
}
}
export function registerGoogleMaps(): ToolRegistration {
const cfg = loadConfig();
const client = new GoogleMapsClient(cfg.googleApiKey || "");
return {
tools: [
{
name: "search_places",
description: "Search places via Google Places Text Search",
inputSchema: {
type: "object",
properties: {
query: { type: "string" },
location: { type: "string", description: "lat,lng (optional)" },
},
required: ["query"],
},
},
{
name: "get_directions",
description: "Get directions between origin and destination",
inputSchema: {
type: "object",
properties: {
origin: { type: "string" },
destination: { type: "string" },
},
required: ["origin", "destination"],
},
},
{
name: "geocode_address",
description: "Geocode a human-readable address",
inputSchema: {
type: "object",
properties: {
address: { type: "string" },
},
required: ["address"],
},
},
],
handlers: {
async search_places(args: Record<string, unknown>) {
if (!cfg.googleApiKey) throw new Error("GOOGLE_API_KEY is not configured");
const query = String(args.query || "");
const location = args.location ? String(args.location) : undefined;
if (!query) throw new Error("query is required");
return client.searchPlaces(query, location);
},
async get_directions(args: Record<string, unknown>) {
if (!cfg.googleApiKey) throw new Error("GOOGLE_API_KEY is not configured");
const origin = String(args.origin || "");
const destination = String(args.destination || "");
if (!origin || !destination) throw new Error("origin and destination are required");
return client.getDirections(origin, destination);
},
async geocode_address(args: Record<string, unknown>) {
if (!cfg.googleApiKey) throw new Error("GOOGLE_API_KEY is not configured");
const address = String(args.address || "");
if (!address) throw new Error("address is required");
return client.geocode(address);
},
},
};
}