Skip to content

tests: add PreRequestHook MCP tool-stamping unit tests for governance plugin - #4298

Merged
akshaydeo merged 3 commits into
devfrom
06-11-tests_mcp_tools_filtering_tests_for_governance
Jun 11, 2026
Merged

tests: add PreRequestHook MCP tool-stamping unit tests for governance plugin#4298
akshaydeo merged 3 commits into
devfrom
06-11-tests_mcp_tools_filtering_tests_for_governance

Conversation

@Pratham-Mishra04

@Pratham-Mishra04 Pratham-Mishra04 commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

Summary

Adds a dedicated test file covering the MCP tool-stamping logic inside PreRequestHook (and the large-payload routing path). The tests verify that the governance plugin correctly enforces virtual key MCP grants — stamping allowed tools onto the request context, pruning caller-supplied tool lists to the granted ceiling, and denying access when no grants exist or the VK is inactive/absent.

Changes

  • Added prerequesthookmcp_test.go with a full decision-matrix test suite covering:
    • Auto-inject enabled/disabled × include-tools present/absent × include-clients present/absent
    • Grant-shape edge cases: no grants, wildcard grants, ungranted client tools, and empty include-tools headers
    • Paths that must not stamp: no virtual key on context, inactive VK, and passthrough requests
    • Large-payload branch (runPreRequestRouting) applying the same stamping rules

Type of change

  • Bug fix
  • Feature
  • Refactor
  • Documentation
  • Chore/CI

Affected areas

  • Core (Go)
  • Transports (HTTP)
  • Providers/Integrations
  • Plugins
  • UI (React)
  • Docs

How to test

go test ./plugins/governance/... -run TestPreRequestHookMCP

All tests should pass with no failures.

Breaking changes

  • Yes
  • No

Related issues

Security considerations

These tests exercise the MCP tool grant enforcement path, which is a security boundary — ensuring callers cannot access tools beyond what their virtual key permits and that inactive or missing VKs cannot be used to bypass grant checks.

Checklist

  • I read docs/contributing/README.md and followed the guidelines
  • I added/updated tests where appropriate
  • I updated documentation where needed
  • I verified builds succeed (Go and UI)
  • I verified the CI pipeline passes locally if applicable

Summary by CodeRabbit

  • Tests
    • Expanded test coverage for governance behavior around automatic tool injection and caller-provided tool filters.
    • Verifies stamping, pruning, and deny-all outcomes across active/inactive contexts, unrestricted grants, and ungranted tool requests.
    • Adds scenarios for empty filter values and large-payload routing while confirming routed model metadata remains unchanged.

Pratham-Mishra04 commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator Author

@coderabbitai

coderabbitai Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 664e5492-be43-44a8-9ee2-93ae877b55de

📥 Commits

Reviewing files that changed from the base of the PR and between fde0b89 and a5a841d.

📒 Files selected for processing (1)
  • plugins/governance/prerequesthookmcp_test.go

📝 Walkthrough

Walkthrough

Adds a comprehensive test file validating GovernancePlugin.PreRequestHook MCP include-tools stamping and pruning across DisableAutoToolInject states, caller MCP filters, virtual key presence/state, grant shapes, and large-payload routing.

Changes

MCP PreRequestHook test coverage

Layer / File(s) Summary
Test infrastructure and context builders
plugins/governance/prerequesthookmcp_test.go
Helper functions construct MCP-enabled virtual keys with optional tool grants, initialize GovernancePlugin with configurable DisableAutoToolInject, build pre-request contexts with caller MCP filters, and execute PreRequestHook while extracting the stamped include-tools value.
Core MCP stamping and pruning logic
plugins/governance/prerequesthookmcp_test.go
Decision-matrix tests for auto-inject vs caller filters: stamping when injection applies, no-stamp when DisableAutoToolInject with no trigger, and pruning when caller include-tools is present; verifies pruned lists are not expanded by grants.
Grant shape and boundary conditions
plugins/governance/prerequesthookmcp_test.go
Edge-case tests for deny-all behavior with absent/empty grants, wildcard grants, pruning to deny-all when caller requests ungranted tools, and empty include-tools header parsing.
Must-NOT-stamp early-return paths
plugins/governance/prerequesthookmcp_test.go
Validates missing virtual key on context, inactive VK, and passthrough request types do not stamp include-tools.
Large-payload routing branch tests
plugins/governance/prerequesthookmcp_test.go
Re-runs stamping/pruning scenarios with LargePayloadMetadata present; asserts include-tools outcomes and verifies routed model metadata remains unchanged (openai/gpt-4o).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • maximhq/bifrost#4282: Related large-payload routing changes; this test asserts routed metadata.Model remains openai/gpt-4o.
  • maximhq/bifrost#3933: Introduced/refactored PreRequestHook MCP injection logic that these tests validate.

Suggested reviewers

  • akshaydeo
  • danpiths

Poem

🐰 I stamp and prune through matrixed test delight,
Headers, grants, and toggles all checked in a night—
Large payloads routed, metadata stays true,
Empty grants deny, wildcards bloom anew,
A bunny cheers for coverage, concise and bright.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding unit tests for the PreRequestHook MCP tool-stamping functionality in the governance plugin.
Description check ✅ Passed The description covers all major sections of the template with appropriate content for a test-focused PR, including summary, changes, type, affected areas, how to test, and security considerations.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch 06-11-tests_mcp_tools_filtering_tests_for_governance

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.12.2)

level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies"


Comment @coderabbitai help to get the list of available commands and usage tips.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@plugins/governance/prerequesthookmcp_test.go`:
- Around line 251-287: Add unit tests using newLargePayloadCtx,
newPluginForMCPStamping, buildVKForMCPStamping, stampedIncludeTools and
newChatRequest to cover the missing large-payload scenarios: (1) auto-inject ON
+ no filters — assert stampedIncludeTools returns the granted tools (stamp
grant), (2) auto-inject ON + include-clients only — assert stamp grant
(include-clients result like the existing AutoInjectOff case but with
auto-inject true), and (3) both include-tools and include-clients present —
assert the pruned include-tools list wins (pruning behavior like
TestPreRequestHookMCP_LargePayload_IncludeToolsPresent_Prunes). Keep test names
consistent with existing pattern (e.g.,
TestPreRequestHookMCP_LargePayload_AutoInjectOn_NoFilters_StampsGrant,
TestPreRequestHookMCP_LargePayload_AutoInjectOn_IncludeClients_StampsGrant,
TestPreRequestHookMCP_LargePayload_BothFilters_Prunes) and reuse
buildVKForMCPStamping tool lists and newChatRequest for inputs.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: dd1daf2b-eedf-4393-a8d6-b75bf8832587

📥 Commits

Reviewing files that changed from the base of the PR and between 21ace8c and a42f33e.

📒 Files selected for processing (1)
  • plugins/governance/prerequesthookmcp_test.go

Comment thread plugins/governance/prerequesthookmcp_test.go
@greptile-apps

greptile-apps Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5

Test-only addition that does not modify production code; all new tests follow existing helper patterns and the production behavior they exercise has been verified against the implementation.

Every test case was traced against the PreRequestHook and runPreRequestRouting implementations — expected values are correct, the VK lifecycle and auto-inject toggle are exercised through the real plugin stack. Previous review concerns about t.Run subtest names, metadata.Model assertions, and explicit-empty-grant coverage are all addressed.

No files require special attention — both coverage suggestions are additive and do not indicate correctness issues with the existing tests.

Important Files Changed

Filename Overview
plugins/governance/prerequesthookmcp_test.go New test file covering PreRequestHook MCP tool-stamping across the full decision matrix; logically correct, addresses previous review concerns about t.Run (#173), metadata.Model assertions (#262), and explicit-empty-grant coverage (#244).

Reviews (2): Last reviewed commit: "tests: mcp tools filtering tests for gov..." | Re-trigger Greptile

Comment thread plugins/governance/prerequesthookmcp_test.go Outdated
Comment thread plugins/governance/prerequesthookmcp_test.go Outdated
Comment thread plugins/governance/prerequesthookmcp_test.go
@Pratham-Mishra04
Pratham-Mishra04 force-pushed the 06-11-tests_mcp_tools_filtering_tests_for_governance branch from a42f33e to fde0b89 Compare June 11, 2026 16:05
@Pratham-Mishra04
Pratham-Mishra04 force-pushed the 06-11-fix_mcp_allowed_clients_governance_fixes branch from 21ace8c to 25f7aaa Compare June 11, 2026 16:05
@Pratham-Mishra04
Pratham-Mishra04 force-pushed the 06-11-tests_mcp_tools_filtering_tests_for_governance branch from fde0b89 to a5a841d Compare June 11, 2026 16:18

akshaydeo commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Merge activity

  • Jun 11, 4:21 PM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Jun 11, 4:26 PM UTC: @akshaydeo merged this pull request with Graphite.

@akshaydeo
akshaydeo changed the base branch from 06-11-fix_mcp_allowed_clients_governance_fixes to graphite-base/4298 June 11, 2026 16:23
@akshaydeo
akshaydeo changed the base branch from graphite-base/4298 to dev June 11, 2026 16:25
@akshaydeo
akshaydeo merged commit 5c3abf1 into dev Jun 11, 2026
10 of 11 checks passed
@akshaydeo
akshaydeo deleted the 06-11-tests_mcp_tools_filtering_tests_for_governance branch June 11, 2026 16:26
akshaydeo pushed a commit that referenced this pull request Jun 12, 2026
…ce plugin (#4298)

## Summary

Adds a dedicated test file covering the MCP tool-stamping logic inside `PreRequestHook` (and the large-payload routing path). The tests verify that the governance plugin correctly enforces virtual key MCP grants — stamping allowed tools onto the request context, pruning caller-supplied tool lists to the granted ceiling, and denying access when no grants exist or the VK is inactive/absent.

## Changes

- Added `prerequesthookmcp_test.go` with a full decision-matrix test suite covering:
  - Auto-inject enabled/disabled × include-tools present/absent × include-clients present/absent
  - Grant-shape edge cases: no grants, wildcard grants, ungranted client tools, and empty include-tools headers
  - Paths that must not stamp: no virtual key on context, inactive VK, and passthrough requests
  - Large-payload branch (`runPreRequestRouting`) applying the same stamping rules

## Type of change

- [ ] Bug fix
- [ ] Feature
- [ ] Refactor
- [ ] Documentation
- [x] Chore/CI

## Affected areas

- [ ] Core (Go)
- [ ] Transports (HTTP)
- [ ] Providers/Integrations
- [x] Plugins
- [ ] UI (React)
- [ ] Docs

## How to test

```sh
go test ./plugins/governance/... -run TestPreRequestHookMCP
```

All tests should pass with no failures.

## Breaking changes

- [ ] Yes
- [x] No

## Related issues

## Security considerations

These tests exercise the MCP tool grant enforcement path, which is a security boundary — ensuring callers cannot access tools beyond what their virtual key permits and that inactive or missing VKs cannot be used to bypass grant checks.

## Checklist

- [ ] I read `docs/contributing/README.md` and followed the guidelines
- [x] I added/updated tests where appropriate
- [ ] I updated documentation where needed
- [x] I verified builds succeed (Go and UI)
- [ ] I verified the CI pipeline passes locally if applicable

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

* **Tests**
  * Expanded test coverage for governance behavior around automatic tool injection and caller-provided tool filters.
  * Verifies stamping, pruning, and deny-all outcomes across active/inactive contexts, unrestricted grants, and ungranted tool requests.
  * Adds scenarios for empty filter values and large-payload routing while confirming routed model metadata remains unchanged.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
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