server.tool(
"greet",
"A simple greeting tool",
{
name: z.string().describe("Name to greet"),
},
async (
args: { name: string },
extra: RequestHandlerExtra<ServerRequest, ServerNotification>,
): Promise<CallToolResult> => {
const apiKey = extra.requestInfo?.headers["x-api-key"];
if (!apiKey) {
return {
content: [
isError: true,
{
type: "text",
text: `You need to have a valid api key to use this tool.`,
},
],
};
}
const yourApiClient = new yourApiClient(apiKey);
const yourApiResponse = await yourApiClient.greet(name);
return {
content: [
{
type: "text",
text: `${yourApiResponse}`,
},
],
};
},
);