get-user
Retrieve WordPress user details by ID using secure REST API integration. Provide site URL, credentials, and user ID to access user information under specified context (view, embed, edit).
Instructions
Get a specific user by ID
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| context | No | Scope under which the request is made | view |
| password | Yes | WordPress application password | |
| siteUrl | Yes | WordPress site URL | |
| userId | Yes | User ID or 'me' for current user | |
| username | Yes | WordPress username |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"context": {
"default": "view",
"description": "Scope under which the request is made",
"enum": [
"view",
"embed",
"edit"
],
"type": "string"
},
"password": {
"description": "WordPress application password",
"type": "string"
},
"siteUrl": {
"description": "WordPress site URL",
"format": "uri",
"type": "string"
},
"userId": {
"anyOf": [
{
"type": "string"
},
{
"type": "number"
},
{
"const": "me",
"type": "string"
}
],
"description": "User ID or 'me' for current user"
},
"username": {
"description": "WordPress username",
"type": "string"
}
},
"required": [
"siteUrl",
"username",
"password",
"userId"
],
"type": "object"
}
Implementation Reference
- src/index.ts:311-339 (handler)Handler function that retrieves a specific WordPress user by ID using the WP REST API via makeWPRequest and returns formatted user details.async ({ siteUrl, username, password, userId, context }) => { try { const user = await makeWPRequest<WPUser>({ siteUrl, endpoint: `users/${userId}`, auth: { username, password }, params: { context } }); return { content: [ { type: "text", text: `User Details:\nID: ${user.id}\nName: ${user.name || "No name"}\nSlug: ${user.slug || "No slug"}\nRoles: ${user.roles?.join(', ') || "No roles"}`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error retrieving user: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } } );
- src/index.ts:304-310 (schema)Zod input schema defining parameters for the get-user tool: siteUrl, credentials, userId (supports 'me'), and optional context.{ siteUrl: z.string().url().describe("WordPress site URL"), username: z.string().describe("WordPress username"), password: z.string().describe("WordPress application password"), userId: z.union([z.string(), z.number(), z.literal("me")]).describe("User ID or 'me' for current user"), context: z.enum(["view", "embed", "edit"]).optional().default("view").describe("Scope under which the request is made"), },
- src/index.ts:301-339 (registration)Registration of the 'get-user' tool on the MCP server, including name, description, input schema, and handler function.server.tool( "get-user", "Get a specific user by ID", { siteUrl: z.string().url().describe("WordPress site URL"), username: z.string().describe("WordPress username"), password: z.string().describe("WordPress application password"), userId: z.union([z.string(), z.number(), z.literal("me")]).describe("User ID or 'me' for current user"), context: z.enum(["view", "embed", "edit"]).optional().default("view").describe("Scope under which the request is made"), }, async ({ siteUrl, username, password, userId, context }) => { try { const user = await makeWPRequest<WPUser>({ siteUrl, endpoint: `users/${userId}`, auth: { username, password }, params: { context } }); return { content: [ { type: "text", text: `User Details:\nID: ${user.id}\nName: ${user.name || "No name"}\nSlug: ${user.slug || "No slug"}\nRoles: ${user.roles?.join(', ') || "No roles"}`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error retrieving user: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } } );
- src/index.ts:59-64 (schema)TypeScript interface defining the structure of a WordPress user object used in the get-user tool response.interface WPUser { id: number; name?: string; slug?: string; roles?: string[]; }
- src/index.ts:158-194 (helper)Helper function makeWPRequest used by get-user to make authenticated HTTP requests to WordPress REST API endpoints.async function makeWPRequest<T>({ siteUrl, endpoint, method = 'GET', auth, data = null, params = null }: { siteUrl: string; endpoint: string; method?: 'GET' | 'POST' | 'PUT' | 'DELETE'; auth: { username: string; password: string }; data?: any; params?: any; }): Promise<T> { const authString = Buffer.from(`${auth.username}:${auth.password}`).toString('base64'); try { const response = await axios({ method, url: `${siteUrl}/wp-json/wp/v2/${endpoint}`, headers: { 'Authorization': `Basic ${authString}`, 'Content-Type': 'application/json', }, data: data, params: params }); return response.data as T; } catch (error) { if (axios.isAxiosError(error) && error.response) { throw new Error(`WordPress API error: ${error.response.data?.message || error.message}`); } throw error; } }