get-category
Retrieve a specific WordPress category by ID using site URL, username, and application password. Integrates with WordPress MCP Server for secure API-based category management.
Instructions
Get a specific category by ID
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| categoryId | Yes | ID of the category to retrieve | |
| context | No | Scope under which the request is made | view |
| password | Yes | WordPress application password | |
| siteUrl | Yes | WordPress site URL | |
| username | Yes | WordPress username |
Implementation Reference
- src/index.ts:1893-1921 (handler)Handler function for the 'get-category' tool. It makes an authenticated GET request to the WordPress REST API endpoint `/wp-json/wp/v2/categories/{categoryId}` and returns formatted category details or an error message.async ({ siteUrl, username, password, categoryId, context }) => { try { const category = await makeWPRequest<WPCategory>({ siteUrl, endpoint: `categories/${categoryId}`, auth: { username, password }, params: { context } }); return { content: [ { type: "text", text: `Category Details:\nID: ${category.id}\nName: ${category.name || "No name"}\nSlug: ${category.slug || "No slug"}\nDescription: ${category.description || "No description"}\nParent ID: ${category.parent || 0}\nPost Count: ${category.count || 0}`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error retrieving category: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } } );
- src/index.ts:1887-1892 (schema)Zod input schema defining parameters for the 'get-category' tool: siteUrl, credentials, categoryId, and optional context.siteUrl: z.string().url().describe("WordPress site URL"), username: z.string().describe("WordPress username"), password: z.string().describe("WordPress application password"), categoryId: z.number().describe("ID of the category to retrieve"), context: z.enum(["view", "embed", "edit"]).optional().default("view").describe("Scope under which the request is made"), },
- src/index.ts:1884-1886 (registration)Registration of the 'get-category' tool on the MCP server instance using server.tool() with name, description, input schema, and handler function."get-category", "Get a specific category by ID", {
- src/index.ts:76-84 (schema)TypeScript interface defining the WPCategory type used for typing the API response in the 'get-category' handler.interface WPCategory { id: number; name?: string; slug?: string; description?: string; parent?: number; count?: number; meta?: Record<string, any>; }
- src/index.ts:158-194 (helper)Shared helper function makeWPRequest used by the 'get-category' handler to perform authenticated requests to the WordPress REST API.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; } }