Skip to content

test(source/mongodb): migrate integration tests to MCP endpoint - #4185

Open
DylanVolz wants to merge 2 commits into
googleapis:mainfrom
DylanVolz:chore/mongodb-mcp-migration
Open

DylanVolz wants to merge 2 commits into
googleapis:mainfrom
DylanVolz:chore/mongodb-mcp-migration

Conversation

@DylanVolz

Copy link
Copy Markdown

Description

This PR migrates the MongoDB integration tests to use the /mcp endpoint on the MCP Toolbox Server.

Key Changes

  • mongodb_mcp_test.go: Migrated legacy /api tests to /mcp

    • TestMongoDBMCPListTools validates the full tools/list manifest for the 18 MongoDB tools in the test config (find, find-one, delete, insert, update and aggregate tools). my-secure-tool is intentionally absent, since tools with secure parameters are excluded from tools/list in this protocol version.
    • TestMongoDBMCPInvokeTool runs the twelve delete, insert, update, aggregate and runtime-collection cases over MCP JSON-RPC. The two rejected invocations (read-only aggregate with $out, and a missing runtime collection) must come back as MCP error results, asserted via AssertMCPError.
    • TestMongoDBMCPCallTool runs the shared fixtures over MCP: RunToolInvokeTest with WithMCP() and RunMCPToolCallMethod. These require a Google ID token, so they are kept separate from the token-free tests above.
    • MongoDB tools return rows, counts, inserted ids and id arrays. The MCP server emits one content block per element for []any results, so the test flattens the content blocks and compares parsed JSON against the REST result (a scalar is compared as a one-element array).
  • mongodb_integration_test.go: Extracted shared helpers so the legacy and MCP tests use the same setup, cases and expected values (no change to the legacy test's assertions):

    • setupMongoDBInstance starts the ephemeral mongo:6 container.
    • seedMongoDB connects and seeds the test collection.
    • getMongoDBInvokeTestCases holds the shared cases (toolName + args map[string]any), and runMongoDBRESTInvokeTests runs them over REST. These replace five near-identical runTool*InvokeTest helpers.
    • The expected values are shared constants
    • Cleanup hardening, applied from review feedback on the other MCP migration PRs:
      • t.Cleanup instead of defer for thelose`), container and collection.
      • Cleanups use context.Background(), since the test context is already cancelled when they run.
      • The Mongo client is now disconnected (
      • The collection drop is registered before seeding, so a failed setup is still cleaned up.
      • REST requests use `http.NewRequestWitht.

No shared test helpers (tests/tool.go, teon.go) are modified. The legacy /apiMongoDB test is kept.

Testing

Verified locally against the mongo:6 testcontainer (Docker only, no Google credentials):

  • TestMongoDBMCPListTools passes: the manimy-secure-tool is excluded.
  • TestMongoDBMCPInvokeTool passes: all 12 MCP invocations, including both error cases.
  • A temporary local probe confirmed:
    • The refactored REST loop (runMongoDBRESTInvokeTests) passes all 12 cases against a server started with --enable-api. - The non-auth tools used by RunToolInvokeTest and RunMCPToolCallMethod (my-simple-tool, my-tool,
      my-tool-by-id, my-tool-by-name, `my-arrarn MCP output matching the shared expectedvalues.
  • Container, collection and client cleanup rs.

Not run locally: TestMongoDBMCPCallTool anEndpoints. They use the shared fixtures thatrequire a Google ID token (gcloud auth print-identity-token`), so they will first run in CI.

PR Checklist

Thank you for opening a Pull Request! Before submitting your PR, there are a
few things you can do to make sure it goes smoothly:

  • Make sure you reviewed
    CONTRIBUTING.md
  • Make sure to open an issue as a
    bug/issue
    before writing your code! That way we can discuss the change, evaluate
    designs, and agree on the general idea
  • Ensure you have manually reviewed the entire diff before requesting a
    review
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)
  • Make sure to add ! if this involve a breaking change

🛠️ Fixes #<issue_number_goes_here>

@DylanVolz
DylanVolz requested a review from a team as a code owner September 29, 2026 20:39
@google-cla

google-cla Bot commented Sep 29, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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

Copy link
Copy Markdown
Contributor

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 refactors the MongoDB integration tests by extracting setup and seeding logic into helper functions, utilizing t.Cleanup for resource management, and consolidating test cases into a shared suite. It also introduces a new test file mongodb_mcp_test.go to run these shared test cases over the Model Context Protocol (MCP). The review feedback suggests improving the teardown process by using context.WithoutCancel(ctx) instead of context.Background() to preserve context values during client disconnection and collection dropping. Additionally, it recommends using InsertMany instead of looping over InsertOne to seed the test database more efficiently.

Comment thread tests/mongodb/mongodb_integration_test.go Outdated
Comment thread tests/mongodb/mongodb_integration_test.go Outdated
Comment thread tests/mongodb/mongodb_integration_test.go Outdated
@AlexTalreja

Copy link
Copy Markdown
Contributor

/gcbrun

@DylanVolz
DylanVolz force-pushed the chore/mongodb-mcp-migration branch from 54a8a23 to 855a8d4 Compare October 16, 2026 23:30

This branch has not been deployed

No deployments
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.

3 participants