wp_get_media
Retrieve a specific media file from a WordPress site by providing its unique ID to access or manage the item directly.
Instructions
Retrieves a single media item by its ID.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| site | No | The ID of the WordPress site to target (from mcp-wordpress.config.json). Required if multiple sites are configured. | |
| id | Yes | The unique identifier for the media item. |
Implementation Reference
- src/tools/media.ts:212-228 (handler)The handler function that implements the core logic of the wp_get_media tool. It takes a WordPressClient and parameters, extracts the media ID, fetches the media item details via client.getMediaItem(id), formats a markdown-style response string with key metadata (title, URL, type, date, alt text, caption), and handles errors by throwing a descriptive message.public async handleGetMedia(client: WordPressClient, params: Record<string, unknown>): Promise<unknown> { const { id } = params as { id: number }; try { const media = await client.getMediaItem(id); const content = `**Media Details (ID: ${media.id})**\n\n` + `- **Title:** ${media.title.rendered}\n` + `- **URL:** ${media.source_url}\n` + `- **Type:** ${media.media_type} (${media.mime_type})\n` + `- **Date:** ${new Date(media.date).toLocaleString()}\n` + (media.alt_text ? `- **Alt Text:** ${media.alt_text}\n` : "") + (media.caption.rendered ? `- **Caption:** ${media.caption.rendered}\n` : ""); return content; } catch (_error) { throw new Error(`Failed to get media item: ${getErrorMessage(_error)}`); } }
- src/tools/media.ts:91-103 (registration)The registration of the wp_get_media tool within the MediaTools.getTools() method's return array. It specifies the tool's name, description, input schema (parameters), and binds the handler function for execution.{ name: "wp_get_media", description: "Retrieves a single media item by its ID.", parameters: [ { name: "id", type: "number", required: true, description: "The unique identifier for the media item.", }, ], handler: this.handleGetMedia.bind(this), },
- src/tools/media.ts:94-101 (schema)The input schema definition for the wp_get_media tool, specifying a single required 'id' parameter of type number with description.parameters: [ { name: "id", type: "number", required: true, description: "The unique identifier for the media item.", }, ],