getUserInfo
Fetch user information from an API by providing a user ID to retrieve specific user data.
Instructions
Fetches user information from an API
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| userId | Yes |
Implementation Reference
- src/tools/example.tool.ts:24-38 (handler)The main handler function for the getUserInfo tool. It parses input, fetches user data using fetchUser, and returns the user info as JSON or an error message.
export async function getUserInfoHandler(input: unknown) { const args = input as { userId: string } try { const user = await fetchUser(args.userId) return { content: [{ type: 'text', text: JSON.stringify(user) }], } } catch (error) { return { content: [{ type: 'text', text: (error as Error).message }], isError: true, } } } - src/tools/example.tool.ts:11-21 (schema)Tool definition including name, description, and input schema that requires a userId string.
export const getUserInfoTool: Tool = { name: 'getUserInfo', description: 'Fetches user information from an API', inputSchema: { type: 'object', properties: { userId: { type: 'string' }, }, required: ['userId'], }, } - src/server.ts:34-44 (registration)Registers the getUserInfoTool in the server's list tools response handler.
server.setRequestHandler(ListToolsRequestSchema, async (request) => { if (isDebug) { console.error( 'Received ListToolsRequest:', JSON.stringify(request, null, 2), ) } return { tools: [getUserInfoTool], } }) - src/server.ts:53-55 (registration)Dispatches tool calls named 'getUserInfo' to the getUserInfoHandler function in the call tool request handler.
if (request.params.name === getUserInfoTool.name) { return getUserInfoHandler(request.params.arguments) } - src/tools/example.tool.ts:4-9 (helper)Helper function that mocks fetching user information based on userId.
async function fetchUser(userId: string) { if (userId === '1') { return { id: '1', name: 'John Doe' } } throw new Error('User not found') }