Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/__tests__/tools/read/get_page_blocks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ describe('get_page_blocks — summary_only short-circuit', () => {
client.getPageBlocks.mockResolvedValue(pageBlocksResponse);
});

it('returns only post_id + summary when summary_only:true', async () => {
it('returns only post_id, summary and notices when summary_only:true', async () => {
const result = await handleReadTool(
'get_page_blocks',
{ post_id: 1, summary_only: true },
client as any
) as Record<string, unknown>;
expect(Object.keys(result).sort()).toEqual(['post_id', 'summary']);
expect(Object.keys(result).sort()).toEqual(['notices', 'post_id', 'summary']);
expect(result.post_id).toBe(1);
expect(result.summary).toBeDefined();
});
Expand Down Expand Up @@ -200,3 +200,26 @@ describe('get_page_blocks — enrichment with legacy blocks', () => {
expect(result.warnings.length).toBeGreaterThan(0);
});
});

describe('get_page_blocks — notices from site plugins', () => {
let client: ReturnType<typeof makeMockClient>;
beforeEach(() => { client = makeMockClient(); });

it('passes the server notices through to the agent', async () => {
client.getPageBlocks.mockResolvedValueOnce({ ...pageBlocksResponse, notices: ['Edit the staged copy #42 instead.'] });
const result = await handleReadTool('get_page_blocks', { post_id: 1 }, client as any) as { notices: string[] };
expect(result.notices).toEqual(['Edit the staged copy #42 instead.']);
});

it('passes notices through in summary_only mode', async () => {
client.getPageBlocks.mockResolvedValueOnce({ summary: pageBlocksResponse.summary, notices: ['Edit #42 instead.'] } as any);
const result = await handleReadTool('get_page_blocks', { post_id: 1, summary_only: true }, client as any) as { notices: string[] };
expect(result.notices).toEqual(['Edit #42 instead.']);
});

it('returns an empty list from a server that sends no notices', async () => {
client.getPageBlocks.mockResolvedValueOnce({ ...pageBlocksResponse });
const result = await handleReadTool('get_page_blocks', { post_id: 1 }, client as any) as { notices: string[] };
expect(result.notices).toEqual([]);
});
});
4 changes: 4 additions & 0 deletions src/agent-guide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ The MCP does this for you:

If the user says "change X on https://example.com/some-page/", your first tool call should be \`get_page_blocks({ url: "...", search: "keyword" })\` or \`resolve_url({ url: "..." })\` — not a shell command.

## Notices: read them before editing

\`get_page_blocks\`, \`get_post_info\` and \`resolve_url\` return \`notices\`: notes from this site's plugins about the post. A URL always resolves to the published page, so a notice may tell you the change belongs somewhere else, such as "a staged copy of this page is waiting; edit #123 instead". Follow it. A write a plugin refuses comes back as an error that says what to do instead.

## Moving / reordering blocks

NEVER do a move as separate \`insert_blocks\` + \`delete_block\` calls — if the delete is skipped or fails, the page ends up with an orphaned clone of the original. The atomic primitive is the \`move\` op on \`edit_block_tree\`:
Expand Down
4 changes: 2 additions & 2 deletions src/tools/discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const DISCOVERY_TOOLS = [
},
{
name: 'resolve_url',
description: 'URL or path → post ID. Accepts full URLs or site-relative paths. Run this before get_page_blocks / update_block / edit_block_tree when you only have a URL.',
description: 'URL or path → post ID. Accepts full URLs or site-relative paths. Run this before get_page_blocks / update_block / edit_block_tree when you only have a URL. Read the returned `notices` before editing.',
annotations: { ...READ_ANNOT, title: 'Resolve URL to post' },
inputSchema: {
type: 'object' as const,
Expand All @@ -127,7 +127,7 @@ export const DISCOVERY_TOOLS = [
},
{
name: 'get_post_info',
description: 'Post metadata by post_id, url, or slug+post_type. Returns `{post_id, title, status, post_url, edit_url, modified, created, parent_id, author, mime_type, comment_count}`. Replaces wp eval / get_permalink() shell-outs.',
description: 'Post metadata by post_id, url, or slug+post_type. Returns `{post_id, title, status, post_url, edit_url, modified, created, parent_id, author, mime_type, comment_count, notices}`. Read `notices` before editing. Replaces wp eval / get_permalink() shell-outs.',
annotations: { ...READ_ANNOT, title: 'Get post info' },
inputSchema: {
type: 'object' as const,
Expand Down
5 changes: 4 additions & 1 deletion src/tools/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const READ_TOOLS = [
{
name: 'get_page_blocks',
description:
"Get a post's blocks. Pass post_id OR url (server resolves URL — don't shell out). Returns `{post_id, summary, blocks[], block_count, warnings}`. Each block: `{index (flat), top_level_counter? (top-level only), path, ref (stable gk_ref), name, attributes, innerHTML?, dynamic, storage_mode (\"static\"|\"dynamic\"|\"dual\"), preference? (when non-preferred)}`. Refs survive sibling shifts — pass them to update_block / delete_block / edit_block_tree to chain mutations without re-fetching. Use outline:true or summary_only:true for cheap inspection.",
"Get a post's blocks. Pass post_id OR url (server resolves URL — don't shell out). Returns `{post_id, summary, blocks[], block_count, warnings, notices}`. Read `notices` before editing: site plugins use them for facts like \"edit the staged copy #123 instead\". Each block: `{index (flat), top_level_counter? (top-level only), path, ref (stable gk_ref), name, attributes, innerHTML?, dynamic, storage_mode (\"static\"|\"dynamic\"|\"dual\"), preference? (when non-preferred)}`. Refs survive sibling shifts — pass them to update_block / delete_block / edit_block_tree to chain mutations without re-fetching. Use outline:true or summary_only:true for cheap inspection.",
annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: true, title: 'Get post blocks' },
outputSchema: {
type: 'object',
Expand All @@ -27,6 +27,7 @@ export const READ_TOOLS = [
blocks: { type: 'array' },
block_count: { type: 'number' },
warnings: { type: 'array' },
notices: { type: 'array', items: { type: 'string' } },
},
},
inputSchema: {
Expand Down Expand Up @@ -174,6 +175,7 @@ export async function handleReadTool(
return {
post_id: postId,
summary: (response as { summary?: unknown }).summary,
notices: (response as { notices?: string[] }).notices ?? [],
};
}

Expand All @@ -188,6 +190,7 @@ export async function handleReadTool(
blocks: enriched.blocks,
block_count: enriched.blocks.length,
warnings: enriched.warnings,
notices: (response as { notices?: string[] }).notices ?? [],
...(pagination !== undefined ? { pagination } : {}),
};
}
Expand Down
17 changes: 12 additions & 5 deletions wordpress-plugin/gk-block-mcp/assets/mcp-server/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -41285,7 +41285,7 @@ var DISCOVERY_TOOLS = [
},
{
name: "resolve_url",
description: "URL or path \u2192 post ID. Accepts full URLs or site-relative paths. Run this before get_page_blocks / update_block / edit_block_tree when you only have a URL.",
description: "URL or path \u2192 post ID. Accepts full URLs or site-relative paths. Run this before get_page_blocks / update_block / edit_block_tree when you only have a URL. Read the returned `notices` before editing.",
annotations: { ...READ_ANNOT, title: "Resolve URL to post" },
inputSchema: {
type: "object",
Expand All @@ -41312,7 +41312,7 @@ var DISCOVERY_TOOLS = [
},
{
name: "get_post_info",
description: "Post metadata by post_id, url, or slug+post_type. Returns `{post_id, title, status, post_url, edit_url, modified, created, parent_id, author, mime_type, comment_count}`. Replaces wp eval / get_permalink() shell-outs.",
description: "Post metadata by post_id, url, or slug+post_type. Returns `{post_id, title, status, post_url, edit_url, modified, created, parent_id, author, mime_type, comment_count, notices}`. Read `notices` before editing. Replaces wp eval / get_permalink() shell-outs.",
annotations: { ...READ_ANNOT, title: "Get post info" },
inputSchema: {
type: "object",
Expand Down Expand Up @@ -41428,7 +41428,7 @@ async function handleDiscoveryTool(toolName, args, client) {
var READ_TOOLS = [
{
name: "get_page_blocks",
description: 'Get a post\'s blocks. Pass post_id OR url (server resolves URL \u2014 don\'t shell out). Returns `{post_id, summary, blocks[], block_count, warnings}`. Each block: `{index (flat), top_level_counter? (top-level only), path, ref (stable gk_ref), name, attributes, innerHTML?, dynamic, storage_mode ("static"|"dynamic"|"dual"), preference? (when non-preferred)}`. Refs survive sibling shifts \u2014 pass them to update_block / delete_block / edit_block_tree to chain mutations without re-fetching. Use outline:true or summary_only:true for cheap inspection.',
description: 'Get a post\'s blocks. Pass post_id OR url (server resolves URL \u2014 don\'t shell out). Returns `{post_id, summary, blocks[], block_count, warnings, notices}`. Read `notices` before editing: site plugins use them for facts like "edit the staged copy #123 instead". Each block: `{index (flat), top_level_counter? (top-level only), path, ref (stable gk_ref), name, attributes, innerHTML?, dynamic, storage_mode ("static"|"dynamic"|"dual"), preference? (when non-preferred)}`. Refs survive sibling shifts \u2014 pass them to update_block / delete_block / edit_block_tree to chain mutations without re-fetching. Use outline:true or summary_only:true for cheap inspection.',
annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: true, title: "Get post blocks" },
outputSchema: {
type: "object",
Expand All @@ -41437,7 +41437,8 @@ var READ_TOOLS = [
summary: { type: "object" },
blocks: { type: "array" },
block_count: { type: "number" },
warnings: { type: "array" }
warnings: { type: "array" },
notices: { type: "array", items: { type: "string" } }
}
},
inputSchema: {
Expand Down Expand Up @@ -41569,7 +41570,8 @@ async function handleReadTool(toolName, args, client) {
if (summaryOnly) {
return {
post_id: postId,
summary: response.summary
summary: response.summary,
notices: response.notices ?? []
};
}
const enriched = enrichBlockList(response.blocks || []);
Expand All @@ -41580,6 +41582,7 @@ async function handleReadTool(toolName, args, client) {
blocks: enriched.blocks,
block_count: enriched.blocks.length,
warnings: enriched.warnings,
notices: response.notices ?? [],
...pagination !== void 0 ? { pagination } : {}
};
}
Expand Down Expand Up @@ -53906,6 +53909,10 @@ The MCP does this for you:

If the user says "change X on https://example.com/some-page/", your first tool call should be \`get_page_blocks({ url: "...", search: "keyword" })\` or \`resolve_url({ url: "..." })\` \u2014 not a shell command.

## Notices: read them before editing

\`get_page_blocks\`, \`get_post_info\` and \`resolve_url\` return \`notices\`: notes from this site's plugins about the post. A URL always resolves to the published page, so a notice may tell you the change belongs somewhere else, such as "a staged copy of this page is waiting; edit #123 instead". Follow it. A write a plugin refuses comes back as an error that says what to do instead.

## Moving / reordering blocks

NEVER do a move as separate \`insert_blocks\` + \`delete_block\` calls \u2014 if the delete is skipped or fails, the page ends up with an orphaned clone of the original. The atomic primitive is the \`move\` op on \`edit_block_tree\`:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@
"name": "resolve_url",
"ability": "gk-block-mcp/resolve-url",
"label": "Resolve URL to post",
"description": "URL or path → post ID. Accepts full URLs or site-relative paths. Run this before get_page_blocks / update_block / edit_block_tree when you only have a URL.",
"description": "URL or path → post ID. Accepts full URLs or site-relative paths. Run this before get_page_blocks / update_block / edit_block_tree when you only have a URL. Read the returned `notices` before editing.",
"input_schema": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -307,7 +307,7 @@
"name": "get_post_info",
"ability": "gk-block-mcp/get-post-info",
"label": "Get post info",
"description": "Post metadata by post_id, url, or slug+post_type. Returns `{post_id, title, status, post_url, edit_url, modified, created, parent_id, author, mime_type, comment_count}`. Replaces wp eval / get_permalink() shell-outs.",
"description": "Post metadata by post_id, url, or slug+post_type. Returns `{post_id, title, status, post_url, edit_url, modified, created, parent_id, author, mime_type, comment_count, notices}`. Read `notices` before editing. Replaces wp eval / get_permalink() shell-outs.",
"input_schema": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -343,7 +343,7 @@
"name": "get_page_blocks",
"ability": "gk-block-mcp/get-page-blocks",
"label": "Get post blocks",
"description": "Get a post's blocks. Pass post_id OR url (server resolves URL — don't shell out). Returns `{post_id, summary, blocks[], block_count, warnings}`. Each block: `{index (flat), top_level_counter? (top-level only), path, ref (stable gk_ref), name, attributes, innerHTML?, dynamic, storage_mode (\"static\"|\"dynamic\"|\"dual\"), preference? (when non-preferred)}`. Refs survive sibling shifts — pass them to update_block / delete_block / edit_block_tree to chain mutations without re-fetching. Use outline:true or summary_only:true for cheap inspection.",
"description": "Get a post's blocks. Pass post_id OR url (server resolves URL — don't shell out). Returns `{post_id, summary, blocks[], block_count, warnings, notices}`. Read `notices` before editing: site plugins use them for facts like \"edit the staged copy #123 instead\". Each block: `{index (flat), top_level_counter? (top-level only), path, ref (stable gk_ref), name, attributes, innerHTML?, dynamic, storage_mode (\"static\"|\"dynamic\"|\"dual\"), preference? (when non-preferred)}`. Refs survive sibling shifts — pass them to update_block / delete_block / edit_block_tree to chain mutations without re-fetching. Use outline:true or summary_only:true for cheap inspection.",
"input_schema": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -414,6 +414,12 @@
},
"warnings": {
"type": "array"
},
"notices": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
Expand Down
6 changes: 6 additions & 0 deletions wordpress-plugin/gk-block-mcp/includes/class-block-reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,13 @@ public function get_blocks( $post_id, $render = false, $persist_refs = true ) {
// silently (no revision) so the refs survive across reads/writes.
// When false, refs are still surfaced in-memory via the cache reference above
// so a second read in the same request sees the same ephemeral refs.
// Saving refs changes post_content, so it obeys the same pre-write
// check as an edit. When a plugin refuses, the read still works with
// in-memory refs and the post is left as it is.
$dirty = $this->crud->assign_missing_refs_recursive( $blocks );
if ( $dirty && $persist_refs && true !== Post_Extensions::check_write( $post_id, 'blocks' ) ) {
$persist_refs = false;
}
if ( $dirty && $persist_refs ) {
// Concurrency guard. Two readers landing on a fresh post could both
// generate random refs and both call persist_ref_assignments(); the
Expand Down
Loading
Loading