Hi there! I’ve tried to use Google Gemini 2.0 flash with MCP in my agent and faced the issue. Everything was working correctly if I used a @modelcontextprotocol/server-filesystem based example but none was working when I switched it to mcp-mongo-server. My database connection string is 100% correct and looks like the agent stuck somewhere at self._arun in the agent.py.
Here’s how my agent initialization code looks like:
async def create_mongodb_agent(session):
"""Create and configure a mongodb agent with MCP tools."""
# Initialize the MCP toolkit
mcp_tools = MCPTools(session=session)
await mcp_tools.initialize()
# Create an agent with the MCP toolkit
return Agent(
model=Gemini(
id="google/gemini-2.0-flash-001",
project_id=PROJECT_ID,
location=LOCATION,
vertexai=True
),
tools=[mcp_tools],
instructions=dedent("""\
You are a MongoDB assistant. Help users explore collections.
- Navigate collections to answer questions
- Use the query, aggregate and count tools to explore collections
- Provide clear context about collections or entities you examine
- Use headings to organize your responses
- Be concise and focus on relevant information\
"""),
markdown=True,
show_tool_calls=True
)
async def run_agent(message: str) -> None:
"""Run mongodb agent with the given message."""
# Initialize the MCP server
server_params = StdioServerParameters(
command="npx",
args=[
"-y",
"mcp-mongo-server",
"mongodb://localhost/mydemodb",
],
)
# Create a client session to connect to the MCP server
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
agent = await create_mongodb_agent(session)
print('running...')
# Run the agent
await agent.aprint_response(message, stream=True)
Also, I can see 7 tools registered for mcp-mongo-server in debugging logs. Do you have any ideas/hints?
I’ve also tried to comment instructions but it didn’t help at all.