-
Notifications
You must be signed in to change notification settings - Fork 41
feat: add explain command #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2febc5d
refactor: remove type assertions and simplify state setup
gagik 6c8752b
fix: tests and token refactor
gagik af90a87
chore: support common.js c
gagik a0bbbe9
feat: add explain tool
gagik 599a179
fix: use union types and reuse argument definitions
gagik e51e7ed
Update src/tools/mongodb/metadata/explain.ts
gagik d78d7f6
fix: remove limit
gagik 5a03f03
Merge branch 'main' of github.com:mongodb-js/mongodb-mcp-server into …
gagik 7bf120e
fix: align with main
gagik 688ad56
fix: linting
gagik 4ab835c
Merge branch 'main' into gagik/add-explain
gagik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; | ||
import { DbOperationArgs, DbOperationType, MongoDBToolBase } from "../mongodbTool.js"; | ||
import { ToolArgs } from "../../tool.js"; | ||
import { z } from "zod"; | ||
import { ExplainVerbosity, Document } from "mongodb"; | ||
import { AggregateArgs } from "../read/aggregate.js"; | ||
import { FindArgs } from "../read/find.js"; | ||
import { CountArgs } from "../read/count.js"; | ||
|
||
export class ExplainTool extends MongoDBToolBase { | ||
protected name = "explain"; | ||
protected description = | ||
"Returns statistics describing the execution of the winning plan chosen by the query optimizer for the evaluated method"; | ||
|
||
protected argsShape = { | ||
...DbOperationArgs, | ||
method: z | ||
.array( | ||
z.union([ | ||
z.object({ | ||
name: z.literal("aggregate"), | ||
arguments: z.object(AggregateArgs), | ||
}), | ||
z.object({ | ||
name: z.literal("find"), | ||
arguments: z.object(FindArgs), | ||
}), | ||
z.object({ | ||
name: z.literal("count"), | ||
arguments: z.object(CountArgs), | ||
}), | ||
]) | ||
) | ||
.describe("The method and its arguments to run"), | ||
}; | ||
|
||
protected operationType: DbOperationType = "metadata"; | ||
|
||
static readonly defaultVerbosity = ExplainVerbosity.queryPlanner; | ||
|
||
protected async execute({ | ||
database, | ||
collection, | ||
method: methods, | ||
}: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> { | ||
const provider = await this.ensureConnected(); | ||
const method = methods[0]; | ||
|
||
if (!method) { | ||
throw new Error("No method provided"); | ||
} | ||
|
||
let result: Document; | ||
switch (method.name) { | ||
case "aggregate": { | ||
const { pipeline } = method.arguments; | ||
result = await provider.aggregate(database, collection, pipeline).explain(ExplainTool.defaultVerbosity); | ||
break; | ||
} | ||
case "find": { | ||
const { filter, ...rest } = method.arguments; | ||
result = await provider | ||
.find(database, collection, filter as Document, { ...rest }) | ||
.explain(ExplainTool.defaultVerbosity); | ||
break; | ||
} | ||
case "count": { | ||
const { query } = method.arguments; | ||
// This helper doesn't have explain() command but does have the argument explain | ||
result = (await provider.count(database, collection, query, { | ||
explain: ExplainTool.defaultVerbosity, | ||
})) as unknown as Document; | ||
break; | ||
} | ||
} | ||
|
||
return { | ||
content: [ | ||
{ | ||
text: `Here is some information about the winning plan chosen by the query optimizer for running the given \`${method.name}\` operation in \`${database}.${collection}\`. This information can be used to understand how the query was executed and to optimize the query performance.`, | ||
type: "text", | ||
}, | ||
{ | ||
text: JSON.stringify(result), | ||
type: "text", | ||
}, | ||
], | ||
}; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know you didn't change this, but I wonder if we should drop limit here? Considering the agg framework has a
$limit
stage, I wonder if that could conflict with a pipeline the model generates.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My concern is the
toArray()
call then. it's going to try to load all the documents from the aggregation result into memoryThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True - just wonder if we might run into trouble if a user asks for give me the top 50 docs and the model generates a stage, but our limit kicks in. Haven't looked into it, but we could check if there's a way to stream the responses to the model.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll remove it and we can revisit with a solution