Skip to content

Commit 976369d

Browse files
jhb-devpaulpopus
andauthored
perf(plugin-mcp): minify JSON in MCP tool responses (#15598)
### What? Replaces `JSON.stringify(result, null, 2)` with `JSON.stringify(result)` in all MCP tool response handlers — resource CRUD, global find/update, auth tools, and job run. ### Why? MCP tool responses are consumed by AI agents, not humans. Pretty-printed JSON with 2-space indentation adds significant whitespace overhead that inflates response size without benefit. For rich-text-heavy documents, this reduces MCP response payload size by ~50%. ### How? - Changed all `JSON.stringify(x, null, 2)` calls to `JSON.stringify(x)` in MCP tool response code (12 source files) - Code-generation files (`config.ts`, `job/create.ts`) are intentionally unchanged — they produce human-readable source files - Updated existing test assertions to match minified format (`"key":"value"` instead of `"key": "value"`) - Added dedicated `Minified JSON responses` test suite verifying JSON blocks contain no indentation and are valid JSON --------- Co-authored-by: Paul Popus <paul@payloadcms.com>
1 parent 3a7c9f3 commit 976369d

13 files changed

Lines changed: 189 additions & 54 deletions

File tree

packages/plugin-mcp/src/mcp/tools/auth/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const authTool = (server: McpServer, req: PayloadRequest, verboseLogs: bo
3939
content: [
4040
{
4141
type: 'text' as const,
42-
text: `# Authentication Status\n\n\`\`\`json\n${JSON.stringify(result, null, 2)}\n\`\`\``,
42+
text: `# Authentication Status\n\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``,
4343
},
4444
],
4545
}

packages/plugin-mcp/src/mcp/tools/auth/forgotPassword.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const forgotPasswordTool = (
3636
content: [
3737
{
3838
type: 'text' as const,
39-
text: `# Password Reset Email Sent\n\n**User:** ${email}\n**Collection:** ${collection}\n**Email Disabled:** ${disableEmail}\n\n\`\`\`json\n${JSON.stringify(result, null, 2)}\n\`\`\``,
39+
text: `# Password Reset Email Sent\n\n**User:** ${email}\n**Collection:** ${collection}\n**Email Disabled:** ${disableEmail}\n\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``,
4040
},
4141
],
4242
}

packages/plugin-mcp/src/mcp/tools/auth/login.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const loginTool = (server: McpServer, req: PayloadRequest, verboseLogs: b
4040
content: [
4141
{
4242
type: 'text' as const,
43-
text: `# Login Successful\n\n**User:** ${email}\n**Collection:** ${collection}\n\n\`\`\`json\n${JSON.stringify(result, null, 2)}\n\`\`\``,
43+
text: `# Login Successful\n\n**User:** ${email}\n**Collection:** ${collection}\n\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``,
4444
},
4545
],
4646
}

packages/plugin-mcp/src/mcp/tools/auth/resetPassword.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const resetPasswordTool = (server: McpServer, req: PayloadRequest, verbos
2929
content: [
3030
{
3131
type: 'text' as const,
32-
text: `# Password Reset Successful\n\n**Collection:** ${collection}\n**Token:** ${token}\n\n\`\`\`json\n${JSON.stringify(result, null, 2)}\n\`\`\``,
32+
text: `# Password Reset Successful\n\n**Collection:** ${collection}\n**Token:** ${token}\n\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``,
3333
},
3434
],
3535
}

packages/plugin-mcp/src/mcp/tools/auth/unlock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const unlockTool = (server: McpServer, req: PayloadRequest, verboseLogs:
3030
content: [
3131
{
3232
type: 'text' as const,
33-
text: `# User Account Unlocked\n\n**User:** ${email}\n**Collection:** ${collection}\n\n\`\`\`json\n${JSON.stringify(result, null, 2)}\n\`\`\``,
33+
text: `# User Account Unlocked\n\n**User:** ${email}\n**Collection:** ${collection}\n\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``,
3434
},
3535
],
3636
}

packages/plugin-mcp/src/mcp/tools/global/find.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export const findGlobalTool = (
8181
type: 'text' as const,
8282
text: `Global "${globalSlug}":
8383
\`\`\`json
84-
${JSON.stringify(result, null, 2)}
84+
${JSON.stringify(result)}
8585
\`\`\``,
8686
},
8787
],

packages/plugin-mcp/src/mcp/tools/global/update.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export const updateGlobalTool = (
120120
type: 'text' as const,
121121
text: `Global "${globalSlug}" updated successfully!
122122
\`\`\`json
123-
${JSON.stringify(result, null, 2)}
123+
${JSON.stringify(result)}
124124
\`\`\``,
125125
},
126126
],

packages/plugin-mcp/src/mcp/tools/job/run.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export const runJob = async (
7979
8080
## Input Data
8181
\`\`\`json
82-
${JSON.stringify(input, null, 2)}
82+
${JSON.stringify(input)}
8383
\`\`\`
8484
8585
## Job Status
@@ -121,7 +121,7 @@ console.log('Job result:', result)
121121
122122
## Input Data Provided:
123123
\`\`\`json
124-
${JSON.stringify(input, null, 2)}
124+
${JSON.stringify(input)}
125125
\`\`\`
126126
127127
## Next Steps:

packages/plugin-mcp/src/mcp/tools/resource/create.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export const createResourceTool = (
114114
text: `Resource created successfully in collection "${collectionSlug}"!
115115
Created resource:
116116
\`\`\`json
117-
${JSON.stringify(result, null, 2)}
117+
${JSON.stringify(result)}
118118
\`\`\``,
119119
},
120120
],

packages/plugin-mcp/src/mcp/tools/resource/delete.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export const deleteResourceTool = (
115115
text: `Document deleted successfully from collection "${collectionSlug}"!
116116
Deleted document:
117117
\`\`\`json
118-
${JSON.stringify(result, null, 2)}
118+
${JSON.stringify(result)}
119119
\`\`\``,
120120
},
121121
],
@@ -148,14 +148,14 @@ Errors: ${errors.length}
148148
if (docs.length > 0) {
149149
responseText += `\n\nDeleted documents:
150150
\`\`\`json
151-
${JSON.stringify(docs, null, 2)}
151+
${JSON.stringify(docs)}
152152
\`\`\``
153153
}
154154

155155
if (errors.length > 0) {
156156
responseText += `\n\nErrors:
157157
\`\`\`json
158-
${JSON.stringify(errors, null, 2)}
158+
${JSON.stringify(errors)}
159159
\`\`\``
160160
}
161161

0 commit comments

Comments
 (0)