how to add this mongo mcp tool to agno
{
“mcpServers”: {
“MongoDB”: {
“command”: “npx”,
“args”: [
“-y”,
“mongodb-mcp-server”,
“–connectionString”,
“mongodb+srv://username:password@cluster.mongodb.net/myDatabase”
]
}
}
}
if i use this i am an output if i test tolls
but if i use it in an agent like below
import asyncio
from textwrap import dedent
from agno.agent import Agent
from agno.models.google import Gemini
from agno.tools.mcp import MCPTools
from dotenv import load_dotenv
import os
Load environment variables
load_dotenv()
MongoDB connection string
MONGO_CONNECTION_STRING = os.getenv(“uri”) # Ensure this is set in your .env file
GOOGLE_API_KEY = os.getenv(“GOOGLE_API_KEY”)
async def run_agent(question: str) → None:
async with (
MCPTools(
f"npx -y mongodb-mcp-server --connectionString {MONGO_CONNECTION_STRING}",
include_tools=[“find”, “aggregate”, “count”],
) as mongo_tools
):
print(“MCPTools initialized successfully.”)
# Create an Agent instance
agent = Agent(
model=Gemini(id="gemini-2.0-flash", api_key=GOOGLE_API_KEY),
tools=[mongo_tools],
instructions=dedent("""\
- Use the `find` tool to retrieve documents from a MongoDB collection.
- Use the `aggregate` tool to perform aggregation queries on a MongoDB collection.
- Use the `count` tool to count documents in a MongoDB collection.
- Always ensure that queries are optimized and follow the schema provided by the database.
- Select the appropriate tool based on the user's question.
- Be concise and focus on relevant information when presenting results.\
"""),
show_tool_calls=True,
markdown=True,
)
# Debug: Check registered tools
print("\n--- Registered Tools in Agent ---")
print(agent.tools)
# Process the user's question
print(f"Processing question: {question}")
try:
# Await the agent's run method
result = await agent.run(question, stream=False)
print(f"Agent Response:\n{result}")
# Debugging: Inspect the tool calls
print("\n--- Tool Calls ---")
for message in result.messages:
if message.role == "tool":
print(f"Tool Name: {message.tool_name}")
print(f"Tool Args: {message.tool_args}")
print(f"Tool Call Error: {message.tool_call_error}")
# Debugging: Inspect the agent's response
print("\n--- Agent Response Details ---")
print(result)
except Exception as e:
print(f"Error processing question: {e}")
Example usage
if name == “main”:
# Example question
question = “List 10 documents in the ‘user’ collection in the ‘callCrm’ database.”
asyncio.run(run_agent(question))
I am getting error as
RunResponse(content=“I’m sorry, I cannot directly display the results in this format. To view the documents, please use a tool that can execute the provided code and display the results.\n”, content_type=‘str’, thinking=None, reasoning_content=None, event=‘RunResponse’, messages=[Message(role=‘system’, content=“\n- Use the find
tool to retrieve documents from a MongoDB collection.\n- Use the aggregate
tool to perform aggregation queries on a MongoDB collection.\n- Use the count
tool to count documents in a MongoDB collection.\n- Always ensure that queries are optimized and follow the schema provided by the database.\n- Select the appropriate tool based on the user’s question.\n- Be concise and focus on relevant information when presenting results. \n\n\n<additional_information>\n- Use markdown to format your answers.\n</additional_information>”, name=None, tool_call_id=None, tool_calls=None, audio=None, images=None, videos=None, files=None, audio_output=None, image_output=None, thinking=None, redacted_thinking=None, provider_data=None, citations=None, reasoning_content=None, tool_name=None, tool_args=None, tool_call_error=None, stop_after_tool_call=False, add_to_agent_memory=True, from_history=False, metrics=MessageMetrics(input_tokens=0, output_tokens=0, total_tokens=0, audio_tokens=0, input_audio_tokens=0, output_audio_tokens=0, cached_tokens=0, reasoning_tokens=0, prompt_tokens=0, completion_tokens=0, prompt_tokens_details=None, completion_tokens_details=None, additional_metrics=None, time=None, time_to_first_token=None, timer=None), references=None, created_at=1747128298), Message(role=‘user’, content=“List all documents in the ‘user’ collection in the ‘callCrm’ database.”, name=None, tool_call_id=None, tool_calls=None, audio=None, images=None, videos=None, files=None, audio_output=None, image_output=None, thinking=None, redacted_thinking=None, provider_data=None, citations=None, reasoning_content=None, tool_name=None, tool_args=None, tool_call_error=None, stop_after_tool_call=False, add_to_agent_memory=True, from_history=False, metrics=MessageMetrics(input_tokens=0, output_tokens=0, total_tokens=0, audio_tokens=0, input_audio_tokens=0, output_audio_tokens=0, cached_tokens=0, reasoning_tokens=0, prompt_tokens=0, completion_tokens=0, prompt_tokens_details=None, completion_tokens_details=None, additional_metrics=None, time=None, time_to_first_token=None, timer=None), references=None, created_at=1747128298), Message(role=‘assistant’, content=None, name=None, tool_call_id=None, tool_calls=[{‘id’: ‘c6abc132-9302-487e-bba0-5bf93302fc54’, ‘type’: ‘function’, ‘function’: {‘name’: ‘find’, ‘arguments’: ‘{“collection”: “user”, “database”: “callCrm”}’}}], audio=None, images=None, videos=None, files=None, audio_output=None, image_output=None, thinking=None, redacted_thinking=None, provider_data=None, citations=None, reasoning_content=None, tool_name=None, tool_args=None, tool_call_error=None, stop_after_tool_call=False, add_to_agent_memory=True, from_history=False, metrics=MessageMetrics(input_tokens=304, output_tokens=6, total_tokens=310, audio_tokens=0, input_audio_tokens=0, output_audio_tokens=0, cached_tokens=0, reasoning_tokens=0, prompt_tokens=0, completion_tokens=0, prompt_tokens_details=None, completion_tokens_details=None, additional_metrics=None, time=2.0888977169997816, time_to_first_token=None, timer=<agno.utils.timer.Timer object at 0x7b15ae710440>), references=None, created_at=1747128298), Message(role=‘tool’, content=[‘<coroutine object get_entrypoint_for_tool..call_tool at 0x7b15ae785580>’], name=None, tool_call_id=None, tool_calls=[{‘tool_name’: ‘find’, ‘content’: ‘<coroutine object get_entrypoint_for_tool..call_tool at 0x7b15ae785580>’}], audio=None, images=None, videos=None, files=None, audio_output=None, image_output=None, thinking=None, redacted_thinking=None, provider_data=None, citations=None, reasoning_content=None, tool_name=None, tool_args=None, tool_call_error=None, stop_after_tool_call=False, add_to_agent_memory=True, from_history=False, metrics=MessageMetrics(input_tokens=0, output_tokens=0, total_tokens=0, audio_tokens=0, input_audio_tokens=0, output_audio_tokens=0, cached_tokens=0, reasoning_tokens=0, prompt_tokens=0, completion_tokens=0, prompt_tokens_details=None, completion_tokens_details=None, additional_metrics=None, time=0.0008152059999702033, time_to_first_token=None, timer=None), references=None, created_at=1747128300), Message(role=‘assistant’, content=“I’m sorry, I cannot directly display the results in this format. To view the documents, please use a tool that can execute the provided code and display the results.\n”, name=None, tool_call_id=None, tool_calls=None, audio=None, images=None, videos=None, files=None, audio_output=None, image_output=None, thinking=None, redacted_thinking=None, provider_data=None, citations=None, reasoning_content=None, tool_name=None, tool_args=None, tool_call_error=None, stop_after_tool_call=False, add_to_agent_memory=True, from_history=False, metrics=MessageMetrics(input_tokens=345, output_tokens=36, total_tokens=381, audio_tokens=0, input_audio_tokens=0, output_audio_tokens=0, cached_tokens=0, reasoning_tokens=0, prompt_tokens=0, completion_tokens=0, prompt_tokens_details=None, completion_tokens_details=None, additional_metrics=None, time=1.018824239999958, time_to_first_token=None, timer=<agno.utils.timer.Timer object at 0x7b15ae785310>), references=None, created_at=1747128300)], metrics={‘input_tokens’: [304, 345], ‘output_tokens’: [6, 36], ‘total_tokens’: [310, 381], ‘audio_tokens’: [0, 0], ‘input_audio_tokens’: [0, 0], ‘output_audio_tokens’: [0, 0], ‘cached_tokens’: [0, 0], ‘reasoning_tokens’: [0, 0], ‘prompt_tokens’: [0, 0], ‘completion_tokens’: [0, 0], ‘time’: [2.0888977169997816, 1.018824239999958]}, model=‘gemini-2.0-flash’, run_id=‘0155b160-0129-4fe0-bfe7-f8b633e7f640’, agent_id=‘55fe06d3-9eba-4916-8888-6b6984e3b7fe’, session_id=‘6af8d990-343c-433b-bd4a-8aaac4af0733’, workflow_id=None, tools=[{‘content’: ‘<coroutine object get_entrypoint_for_tool..call_tool at 0x7b15ae785580>’, ‘tool_call_id’: ‘c6abc132-9302-487e-bba0-5bf93302fc54’, ‘tool_name’: ‘find’, ‘tool_args’: {‘collection’: ‘user’, ‘database’: ‘callCrm’}, ‘tool_call_error’: False, ‘metrics’: MessageMetrics(input_tokens=0, output_tokens=0, total_tokens=0, audio_tokens=0, input_audio_tokens=0, output_audio_tokens=0, cached_tokens=0, reasoning_tokens=0, prompt_tokens=0, completion_tokens=0, prompt_tokens_details=None, completion_tokens_details=None, additional_metrics=None, time=0.0008152059999702033, time_to_first_token=None, timer=None), ‘created_at’: 1747128300}], formatted_tool_calls=[‘find(collection=user, database=callCrm)’], images=None, videos=None, audio=None, response_audio=None, citations=None, extra_data=None, created_at=1747128296)