Skip to content

feat: add structured content for response in mongodb tools - MCP-374#1249

Open
dudaschar wants to merge 27 commits into
mainfrom
MCP-374-structuredContent
Open

feat: add structured content for response in mongodb tools - MCP-374#1249
dudaschar wants to merge 27 commits into
mainfrom
MCP-374-structuredContent

Conversation

@dudaschar

@dudaschar dudaschar commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

This work enables the output to be provided in structuredContent format which caters for the prompt injection case, but removes the need for additional work on the users side to re-format the output to remove the tags.

It also add serialization safety when parsing BSON documents into JSON, to values type long are now properly handled in the Tool output. A follow up ticket will be created to discuss Tool arguments and interpolation in the output text response.

This PR apply the updates for the tools inside the mongodb package, a follow up PR will handle /atlas and /atlas-local

Checklist

@dudaschar dudaschar self-assigned this Jun 15, 2026
Comment thread src/tools/mongodb/read/export.ts Outdated
Comment thread src/tools/tool.ts

type StructuredToolResult<OutputSchema extends ZodRawShape> = {
content: { type: "text"; text: string }[];
content: CallToolResult["content"];

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is allowing the tools to with a previous custom tool result to have a content other than only text + structured content without the need to custom types or casting the output type.

Comment thread src/helpers/bsonToJson.ts
*
* ObjectId becomes `{ "$oid": "..." }`, Long becomes `{ "$numberLong": "..." }`, etc.
*/
export function bsonToJson(value: Record<string, unknown>): Record<string, unknown>;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is overloading is supporting dbStats tool, which is an object instead of a collection document, so no cast in the tool will be necessary (or any other similar).

@dudaschar dudaschar marked this pull request as ready for review June 16, 2026 16:35
@dudaschar dudaschar requested a review from a team as a code owner June 16, 2026 16:35
@dudaschar dudaschar requested review from Luke-Sanderson and lerouxb and removed request for a team June 16, 2026 16:35
@dudaschar dudaschar changed the title feat: add structured content for response in mongodb tools [MCP-374] feat: add structured content for response in mongodb tools - MCP-374 Jun 16, 2026
Comment on lines +60 to +69
protected override async handleError(
error: unknown,
args: ToolArgs<typeof this.argsShape>
): Promise<CallToolResult> {
const result = await super.handleError(error, args);
if (result.isError) {
return { ...result, structuredContent: { connected: false } };
}
return result;
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option here would be not handling the error, and structured content be undefined, instead of returning false. If we decide to return undefined, I'll update similar tools to do the same.

@nirinchev nirinchev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good - I have mostly stylistic comments/questions, that are not critical, but good to address. We should probably revert the export changes though as it's the kind of tool that doesn't benefit much (if at all) by adding structured content.

Comment thread src/helpers/bsonToJson.ts Outdated
Comment thread src/helpers/constants.ts Outdated
Comment thread src/tools/mongodb/read/aggregate.ts Outdated
documents: z.array(z.unknown()).describe("The documents returned by the aggregation pipeline"),
aggResultsCount: z
.number()
.optional()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this optional? Wouldn't we always be able to supply the count?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

totalDocuments can be null, if something downstream fails in the countAggregationResultDocuments, the value will be undefined (not 0).

Not sure how often this can actually happen or if we wanna handle the response in a different way. I don't think we should return as 0 tho, because it could be misleading.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see - wonder if that will be clear enough for the LLM - we could also have it as union type of number and the const string indeterminate or something similar to be extra explicit?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is triggered by an error, I think "not_available" could be better, so it's also a hint to the llm to retry the tool if count is necessary. But not a hard opinion here, tho.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the existing message already uses indeterminable number of documents, so I'll keep indeterminate for consistency now.

Comment thread src/tools/mongodb/read/aggregate.ts Outdated
Comment thread src/tools/mongodb/read/aggregate.ts Outdated
Comment thread tests/integration/tools/mongodb/read/export.test.ts Outdated
Comment thread src/tools/mongodb/read/export.ts Outdated
Comment thread src/tools/mongodb/read/find.ts Outdated
Comment thread tests/integration/tools/mongodb/read/aggregate.test.ts Outdated
Comment thread tests/integration/tools/mongodb/read/aggregate.test.ts Outdated
@dudaschar dudaschar requested a review from nirinchev June 25, 2026 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants