DEV Community

Cover image for Leveraging Skills in a MongoDB MCP Agent
MongoDB Guests for MongoDB

Posted on

Leveraging Skills in a MongoDB MCP Agent

This article was written by Jack Woehr.

SoftWoehr LLC - Seiden Group - ProCern Technology Solutions.


Skills in a ReAct (Reason - Act) Agent for MongoDB

In this article, we describe our open-source mymongodbmcpagent implementing a ReAct (Reason - Act) agent leveraging the MongoDB MCP Server and the MongoDB agent-skills package. Our code also provides a chat interface so that you can talk to your MongoDB instance and offer queries whose answers require strategies derived from the skills package, leading to multiple exchanges between the agent and the LLM.

Evolving from a one-pass MongoDB Server Agent

If you read my previous article, A Simple MongoDB Server Agent, you saw an agent/chat pair using an LLM provider to answer one-pass queries to MongoDB via the MongoDB MCP Server. I used that architecture to add MongoDB capabilities to a multi-agent dashboard that connects to many different projects I work on.

Reuse the scripting from the earlier article

Keep a link to that earlier article because its associated code on GitHub includes a nice script and .env.example to start the MongoDB MCP server, which can be used with the code we're presenting in this article.

Prerequisite

As in the earlier article, we assume you have a MongoDB Atlas Cluster with some sample data to query.

Getting Started

A full Getting Started document is available in the repository to help you set up.

Add Skills to MCP

  • MCP tools are … tools! An LLM has no arms and legs. All it can do is talk, but once informed that there are tools (provided by the MCP server) and the skills to use them, it can call for the tools to be used based on its decisions.
  • Skills are instructions for using tools to accomplish tasks that require more than one tool call. They're in text, in Markdown format, and are prompts, plain English instructions for the language model to follow. Take a look at the skills directory of the repo just mentioned.
  • The Agent mediates between
    • the User
    • the Inference Engine (the LLM)
    • the tools and skills
      • loading the tools list for the LLM
      • loading the skills list for the LLM
      • calling the tools on behalf of the LLM

Skills add complexity to the Agent

Using skills requires the agent to execute a ReAct (Reasoning + Acting) loop that supports multiple LLM tool calls until it arrives at a response.

The enhanced ReAct loop agent

The code for the enhanced agent and its associated chat REPL is found at mymongodbmcpagent. Pay close attention to the .env.example file and create a .env file so that the agent and chat will both load and function as expected.

The diagram below shows the flow of the agent.

The payoff: more sophisticated queries

In the screenshot below, with the agent (and, of course, the MongoDB MCP server) running, I have asked "find me pottery mugs".

Answering this query requires that the LLM use its skills to exercise the ReAct loop to:

  • list the databases in the Atlas installation we've pointed it to
  • list collections in those databases looking for something useful ("Ha! There's a collection called pottery in the herwheel3_test database!")
  • work out how to find "mugs" in that collection ("Hmm, there are arrays of categories and tags in these documents …")

As the Agent itself prints some of its processing to standard out, we can see that it took 6 iterations:

[req=72b91571-d634-4597-a54a-6b992939335a] [ReAct] Iteration 1...
[req=72b91571-d634-4597-a54a-6b992939335a] [ReAct] Tool selected: list-databases
[req=72b91571-d634-4597-a54a-6b992939335a] Session may have expired, attempting to reconnect...
[req=72b91571-d634-4597-a54a-6b992939335a] ✓ Successfully initialized session with MongoDB MCP server
[req=72b91571-d634-4597-a54a-6b992939335a] Reconnected, retrying tool call...
[req=72b91571-d634-4597-a54a-6b992939335a] [ReAct] Iteration 2...
[req=72b91571-d634-4597-a54a-6b992939335a] [ReAct] Tool selected: list-collections
[req=72b91571-d634-4597-a54a-6b992939335a] [ReAct] Iteration 3...
[req=72b91571-d634-4597-a54a-6b992939335a] [ReAct] Tool selected: find
[req=72b91571-d634-4597-a54a-6b992939335a] [ReAct] Iteration 4...
[req=72b91571-d634-4597-a54a-6b992939335a] [ReAct] Tool selected: collection-schema
[req=72b91571-d634-4597-a54a-6b992939335a] [ReAct] Iteration 5...
[req=72b91571-d634-4597-a54a-6b992939335a] [ReAct] Tool selected: find
[req=72b91571-d634-4597-a54a-6b992939335a] [ReAct] Iteration 6...
[req=72b91571-d634-4597-a54a-6b992939335a] [ReAct] Final answer reached.
Enter fullscreen mode Exit fullscreen mode

The secret sauce in the agent-server.js code

The prompt reveals how the LLM interacts with the agent to make multiple tool calls and then signal when it has arrived at a final response:

  let currentPrompt = `You are an expert MongoDB autonomous agent.
Your goal is to answer the user's question by taking actions and analyzing their results.


Here is a list of available tools:
${formattedTools}


${loadedSkills ? `You have the following skills available to guide your actions:\n${loadedSkills}\n` : ''}
User's request: "${question}"


Decide what to do next based on the request.
- If you need to use a tool to gather more information or execute a step, return a JSON object with "action": "tool", "toolName": "<name>", and "toolArguments": {<args>}.
- If you have gathered enough information and can answer the user's request (or if no tools are needed), return a JSON object with "action": "answer", and "finalAnswer": "<your natural language answer>".


Your response MUST be exactly ONE valid JSON object and nothing else. Do not use markdown blocks like \`\`\`json.`;
Enter fullscreen mode Exit fullscreen mode


json
You'll see in the source code that agent-server.js checks the incoming JSON on each pass through the LLM's responses, where it executes tool calls and watches for that finalAnswer indicator. Naturally, there is a configurable '.env ' limit to the number of iterations so you don't spend your rent money on a wedged inference.

Agentic code assistance

This project is extracted from a component of a larger project (my Dashboard, not yet released) and was coded over several months in spare time using the following agentic code assistants (listed in alphabetical order):

Variety may well be the spice of life, but it's also an essential oil for lubricating one's experience with agentic code assistance as this field rapidly evolves.

Summary

  • MongoDB MCP server is not just a plugin to code assist; it's an application building block.
  • MongoDB MCP server is best leveraged using skills.
  • Skills require the agent to present a ReAct (Reasoning + Acting) architecture.
  • This article is accompanied by code.
    • The code supports several popular LLM providers.
    • The code is capable of servicing multiple concurrent requests.

Top comments (0)