Add read-only policy support to MCP tool requests (#46042)

## Why

Read-only MCP discovery and invocation need a consistent policy across requests, connection reuse, and tool catalogs. Reusing an unrestricted connection or cached catalog can bypass filtered discovery.

## What changed

- Thread `requires_read_only_mcp_tools` through MCP configuration and client setup, leaving it disabled by default.
- When enabled, set `openai/readOnly` to `true` in `tools/list` and `tools/call` request metadata, overriding a caller-supplied `false` while preserving other metadata, pagination, and arguments.
- Include the policy in connection identity and bypass shared Apps and persistent tool catalog caches for read-only connections.

## Testing

Add coverage for metadata propagation in legacy and modern protocol modes, preservation through session recovery, and isolation from unrestricted connections and shared Apps catalogs.

GitOrigin-RevId: f4a3a4021a54e302bffd53f08092b080c056246b
This commit is contained in:
Rennie authored and copyberry committed 2026-09-16 22:25:54 +00:00
1 parent 78e7825a47
commit b97abdbe30
15 files changed
+312 -42

No files matched your search

@@ -59,6 +59,7 @@ fn lite_tool_catalog_and_code_calls_are_visible() {
json!({ "type": "custom_tool_call_output", "output": {
"content": "plan updated", "success": true
} }),
json!({ "type": "function_call_output", "output": "Wall time: 0.0025 seconds\nOutput: Report ready." }),
],
&ContextSnapshotOptions::default(),
);
@@ -72,6 +73,7 @@ fn lite_tool_catalog_and_code_calls_are_visible() {
));
assert!(rendered.contains(" | ready"));
assert!(rendered.contains("03:custom_tool_call_output:success=true:plan updated"));
assert!(rendered.contains("04:function_call_output:Wall time: <DURATION> seconds"));
}
#[test]
@@ -109,15 +109,17 @@ impl Normalizer {
}
fn normalize_values(&mut self, text: &str) -> String {
// Code Mode reports elapsed wall time in an otherwise stable tool output header.
let text = if text.starts_with("Script completed\nWall time ") {
// Tool calls report elapsed wall time in an otherwise stable output header.
let text = if text.starts_with("Script completed\nWall time ")
|| text.starts_with("Wall time: ")
{
static WALL_TIME: OnceLock<Regex> = OnceLock::new();
WALL_TIME
.get_or_init(|| {
Regex::new(r"(?m)^Wall time [0-9]+(?:\.[0-9]+)? seconds$")
.expect("code mode wall time regex")
Regex::new(r"(?m)^(Wall time:?) [0-9]+(?:\.[0-9]+)? seconds$")
.expect("tool wall time regex")
})
.replace(text, "Wall time <DURATION> seconds")
.replace(text, "${1} <DURATION> seconds")
.into_owned()
} else {
text.to_string()