Skip to content

improve: enhance tool descriptions for better LLM agent tool selection#37

Open
spidershield-contrib wants to merge 2 commits intokiliczsh:mainfrom
spidershield-contrib:improve/tool-descriptions
Open

improve: enhance tool descriptions for better LLM agent tool selection#37
spidershield-contrib wants to merge 2 commits intokiliczsh:mainfrom
spidershield-contrib:improve/tool-descriptions

Conversation

@spidershield-contrib
Copy link
Copy Markdown

What

Rewrote all 8 MCP tool descriptions using an action-verb-first format designed for LLM agent planning.

Why

MCP tool descriptions serve as planning hints — LLMs use them to select which tool to call and in what order. The current single-sentence descriptions (e.g., "Update documents in a MongoDB collection") don't help LLMs distinguish between query vs. aggregate, or update vs. insert, leading to tool selection errors.

Format applied

Each description now includes:

  • Action verb lead — what the tool does
  • Use when — concrete scenarios for this tool
  • Do not use when — disambiguation with sibling tools (e.g., "use aggregate instead")
  • Accepts — key parameters with types
  • Raises an error if — failure conditions

Example: query (before → after)

Before:

Execute a MongoDB query with optional execution plan analysis

After:

Execute a MongoDB find query to retrieve documents from a collection, with optional execution plan analysis.

Use when the user wants to read, search, or filter documents in a specific collection.
Do not use when you need to perform multi-stage data transformations (use aggregate instead) or when you want to modify data (use update or insert instead).
Accepts 'collection' (required), 'filter' (optional query object), 'projection' (optional field selection), 'limit' (optional, default 10), and 'explain' (optional, for query plan analysis).
Raises an error if the collection does not exist or the filter syntax is invalid.

Scope

  • 1 file changed (src/schemas/tools.ts)
  • 8 tool descriptions rewritten
  • Pure string replacement — no logic, schema, or API changes
  • Build verified (npm run build ✓), lint verified (npm run lint ✓)

🤖 Descriptions improved with spidershield — open-source MCP security & quality scanner.

Rewrite all 8 MCP tool descriptions using an action-verb-first format
designed for LLM agent planning. Each description now includes:

- What the tool does (action verb lead)
- When to use it (concrete scenarios)
- When NOT to use it (disambiguation with sibling tools)
- Key parameters with examples
- Error conditions

This helps LLM agents select the correct tool on the first attempt,
reducing tool selection errors in multi-step database workflows.
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the clarity and utility of MongoDB tool descriptions for LLM agents. By adopting a structured, action-verb-first format with detailed usage guidelines, the changes aim to minimize tool selection errors and improve the overall planning capabilities of AI systems interacting with MongoDB.

Highlights

  • Enhanced LLM Agent Tool Descriptions: Rewrote all 8 MongoDB tool descriptions to an "action-verb-first" format, specifically designed to improve LLM agent planning and tool selection accuracy.
  • Detailed Description Structure: Each tool description now includes explicit sections for "Use when" (concrete scenarios), "Do not use when" (disambiguation), "Accepts" (key parameters), and "Raises an error if" (failure conditions).

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/schemas/tools.ts
    • Rewrote descriptions for 8 MongoDB tools (query, aggregate, update, serverInfo, insert, createIndex, count, listCollections) to improve clarity for LLM agents.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request significantly improves the tool descriptions to aid LLM agent planning. The new detailed format is a great enhancement. My review focuses on ensuring the descriptions are complete and consistent. I've found that several tool descriptions are missing some of their available parameters, which could limit the LLM's ability to use them effectively. I've also suggested using template literals for multi-line strings to improve code readability and maintainability.

Comment on lines +20 to +24
"Execute a MongoDB find query to retrieve documents from a collection, with optional execution plan analysis.\n\n" +
"Use when the user wants to read, search, or filter documents in a specific collection.\n" +
"Do not use when you need to perform multi-stage data transformations (use aggregate instead) or when you want to modify data (use update or insert instead).\n" +
"Accepts 'collection' (required), 'filter' (optional query object), 'projection' (optional field selection), 'limit' (optional, default 10), and 'explain' (optional, for query plan analysis).\n" +
"Raises an error if the collection does not exist or the filter syntax is invalid.",
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The tool description is missing the objectIdMode parameter in the Accepts section. For the LLM to be aware of all available parameters, it's important to list them all.

Additionally, for better readability and maintainability, I suggest using a template literal for the multi-line description string instead of string concatenation.

          `Execute a MongoDB find query to retrieve documents from a collection, with optional execution plan analysis.

Use when the user wants to read, search, or filter documents in a specific collection.
Do not use when you need to perform multi-stage data transformations (use aggregate instead) or when you want to modify data (use update or insert instead).
Accepts 'collection' (required), 'filter' (optional query object), 'projection' (optional field selection), 'limit' (optional, default 10), 'explain' (optional, for query plan analysis), and 'objectIdMode' (optional, default 'auto').
Raises an error if the collection does not exist or the filter syntax is invalid.`,

Comment on lines +64 to +68
"Execute a MongoDB aggregation pipeline to perform multi-stage data transformations, grouping, joins, or analytics.\n\n" +
"Use when the user needs $group, $lookup, $unwind, $match chains, or any computation that goes beyond simple filtering.\n" +
"Do not use when a simple find query with filter and projection is sufficient (use query instead).\n" +
"Accepts 'collection' (required), 'pipeline' (required, array of stage objects), and 'explain' (optional, for execution plan analysis).\n" +
"Raises an error if the pipeline syntax is invalid or a stage operator is unrecognized.",
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The tool description is missing the objectIdMode parameter in the Accepts section. For the LLM to be aware of all available parameters, it's important to list them all.

Additionally, for better readability and maintainability, I suggest using a template literal for the multi-line description string instead of string concatenation.

          `Execute a MongoDB aggregation pipeline to perform multi-stage data transformations, grouping, joins, or analytics.

Use when the user needs $group, $lookup, $unwind, $match chains, or any computation that goes beyond simple filtering.
Do not use when a simple find query with filter and projection is sufficient (use query instead).
Accepts 'collection' (required), 'pipeline' (required, array of stage objects), 'explain' (optional, for execution plan analysis), and 'objectIdMode' (optional, default 'auto').
Raises an error if the pipeline syntax is invalid or a stage operator is unrecognized.`,

Comment on lines +103 to +107
"Update one or more documents in a MongoDB collection using update operators ($set, $unset, $inc, etc.).\n\n" +
"Use when the user wants to modify existing documents — change field values, increment counters, or remove fields.\n" +
"Do not use when you need to add new documents (use insert instead) or when you need to read data (use query instead).\n" +
"Accepts 'collection' (required), 'filter' (required), 'update' (required, update operators), 'upsert' (optional), and 'multi' (optional, for updating multiple documents).\n" +
"Raises an error if the filter or update syntax is invalid.",
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The tool description is missing the objectIdMode parameter in the Accepts section. For the LLM to be aware of all available parameters, it's important to list them all.

Additionally, for better readability and maintainability, I suggest using a template literal for the multi-line description string instead of string concatenation.

          `Update one or more documents in a MongoDB collection using update operators ($set, $unset, $inc, etc.).

Use when the user wants to modify existing documents — change field values, increment counters, or remove fields.
Do not use when you need to add new documents (use insert instead) or when you need to read data (use query instead).
Accepts 'collection' (required), 'filter' (required), 'update' (required, update operators), 'upsert' (optional), 'multi' (optional, for updating multiple documents), and 'objectIdMode' (optional, default 'auto').
Raises an error if the filter or update syntax is invalid.`,

Comment on lines +166 to +170
"Insert one or more new documents into a MongoDB collection.\n\n" +
"Use when the user wants to add new records, seed data, or create documents in a collection.\n" +
"Do not use when you need to modify existing documents (use update instead) or when you need to read data (use query instead).\n" +
"Accepts 'collection' (required), 'documents' (required, array of document objects), 'ordered' (optional), and 'writeConcern' (optional).\n" +
"Raises an error if the collection name is invalid or document validation fails.",
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The tool description is missing the bypassDocumentValidation and objectIdMode parameters in the Accepts section. For the LLM to be aware of all available parameters, it's important to list them all.

Additionally, for better readability and maintainability, I suggest using a template literal for the multi-line description string instead of string concatenation.

          `Insert one or more new documents into a MongoDB collection.

Use when the user wants to add new records, seed data, or create documents in a collection.
Do not use when you need to modify existing documents (use update instead) or when you need to read data (use query instead).
Accepts 'collection' (required), 'documents' (required, array of document objects), 'ordered' (optional), 'writeConcern' (optional), 'bypassDocumentValidation' (optional), and 'objectIdMode' (optional, default 'auto').
Raises an error if the collection name is invalid or document validation fails.`,

Comment on lines +209 to +213
"Create one or more indexes on a MongoDB collection to improve query performance.\n\n" +
"Use when the user wants to add indexes for frequently queried fields, enforce uniqueness, or set up TTL expiration on documents.\n" +
"Do not use when you need to query or modify data (use query, update, or insert instead).\n" +
"Accepts 'collection' (required) and 'indexes' (required, array of index specs with key patterns). Each index supports options like 'unique', 'sparse', 'expireAfterSeconds', and 'partialFilterExpression'.\n" +
"Raises an error if the index key pattern is invalid or the collection does not exist.",
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The tool description is missing the writeConcern, commitQuorum, and objectIdMode parameters in the Accepts section. The description of options for index specs is also incomplete. For the LLM to be aware of all available parameters, it's important to list them all.

Additionally, for better readability and maintainability, I suggest using a template literal for the multi-line description string instead of string concatenation.

          `Create one or more indexes on a MongoDB collection to improve query performance.

Use when the user wants to add indexes for frequently queried fields, enforce uniqueness, or set up TTL expiration on documents.
Do not use when you need to query or modify data (use query, update, or insert instead).
Accepts 'collection' (required), 'indexes' (required, array of index specs), 'writeConcern' (optional), 'commitQuorum' (optional), and 'objectIdMode' (optional, default 'auto'). Each index spec requires a 'key' and supports options like 'name', 'unique', 'sparse', 'background', 'expireAfterSeconds', and 'partialFilterExpression'.
Raises an error if the index key pattern is invalid or the collection does not exist.`,

Comment on lines +280 to +284
"Count the number of documents in a collection that match a query filter.\n\n" +
"Use when the user wants to know how many documents exist, check if data is present, or get collection statistics.\n" +
"Do not use when you need to retrieve the actual documents (use query instead) or perform aggregation counts with grouping (use aggregate instead).\n" +
"Accepts 'collection' (required), 'query' (optional filter), 'limit' (optional max count), and 'skip' (optional docs to skip).\n" +
"Raises an error if the collection does not exist or the query filter is invalid.",
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The tool description is missing several parameters in the Accepts section: hint, readConcern, maxTimeMS, collation, and objectIdMode. For the LLM to be aware of all available parameters, it's important to list them all.

Additionally, for better readability and maintainability, I suggest using a template literal for the multi-line description string instead of string concatenation.

          `Count the number of documents in a collection that match a query filter.

Use when the user wants to know how many documents exist, check if data is present, or get collection statistics.
Do not use when you need to retrieve the actual documents (use query instead) or perform aggregation counts with grouping (use aggregate instead).
Accepts 'collection' (required), 'query' (optional filter), 'limit' (optional max count), 'skip' (optional docs to skip), 'hint' (optional), 'readConcern' (optional), 'maxTimeMS' (optional), 'collation' (optional), and 'objectIdMode' (optional, default 'auto').
Raises an error if the collection does not exist or the query filter is invalid.`,

Comment on lines +315 to +319
"List all collections in the connected MongoDB database.\n\n" +
"Use when the user wants to explore available collections before querying, or to verify a collection exists.\n" +
"Do not use when you already know the collection name and need to query its documents (use query instead).\n" +
"Accepts 'nameOnly' (optional boolean, returns only collection names for a compact listing) and 'filter' (optional, to filter collections by properties).\n" +
"Raises an error if the database connection is unavailable.",
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The tool description is missing the objectIdMode parameter in the Accepts section. For the LLM to be aware of all available parameters, it's important to list them all.

Additionally, for better readability and maintainability, I suggest using a template literal for the multi-line description string instead of string concatenation.

          `List all collections in the connected MongoDB database.

Use when the user wants to explore available collections before querying, or to verify a collection exists.
Do not use when you already know the collection name and need to query its documents (use query instead).
Accepts 'nameOnly' (optional boolean, returns only collection names for a compact listing), 'filter' (optional, to filter collections by properties), and 'objectIdMode' (optional, default 'auto').
Raises an error if the database connection is unavailable.`,

Comment on lines +147 to +151
"Retrieve MongoDB server metadata including version, storage engine, host, and uptime.\n\n" +
"Use when the user wants to check the database server version, verify the storage engine, or diagnose server-level configuration.\n" +
"Do not use when you need to list collections (use listCollections instead) or query data (use query instead).\n" +
"Accepts 'includeDebugInfo' (optional boolean, includes additional diagnostic details).\n" +
"Raises an error if the server is unreachable.",
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

For consistency with other descriptions and for better readability, I suggest using a template literal for this multi-line description string instead of string concatenation.

          `Retrieve MongoDB server metadata including version, storage engine, host, and uptime.

Use when the user wants to check the database server version, verify the storage engine, or diagnose server-level configuration.
Do not use when you need to list collections (use listCollections instead) or query data (use query instead).
Accepts 'includeDebugInfo' (optional boolean, includes additional diagnostic details).
Raises an error if the server is unreachable.`,

…e literals

Address code review feedback from Gemini Code Assist:
- Add missing 'objectIdMode' parameter to all 7 tool descriptions
- Add missing 'bypassDocumentValidation' to insert description
- Add missing 'writeConcern', 'commitQuorum' to createIndex description
- Add missing 'hint', 'readConcern', 'maxTimeMS', 'collation' to count description
- Add missing 'filter' to listCollections description
- Convert all descriptions from string concatenation to template literals
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant