Skip to content

Allow MCP agent to override CLI and Config log levels (Agent > CLI > Config)#3544

Open
anushakolan wants to merge 7 commits into
mainfrom
dev/anushakolan/log_level_precedence_order
Open

Allow MCP agent to override CLI and Config log levels (Agent > CLI > Config)#3544
anushakolan wants to merge 7 commits into
mainfrom
dev/anushakolan/log_level_precedence_order

Conversation

@anushakolan
Copy link
Copy Markdown
Contributor

@anushakolan anushakolan commented May 12, 2026

Why make this change?

Closes #3533Log-level precedence should be Agent > CLI > Config; MCP logging/setLevel is silently ignored when CLI/Config set a level.

The runtime today implements CLI > Config > Agent: once --LogLevel is passed or runtime.telemetry.log-level is set, the agent's request silently no-ops. There is no way for an agent to dial verbosity up or down at runtime, which defeats the purpose of MCP logging/setLevel.

The team locked the corrected precedence on 5/1 : Agent > CLI > Config > defaults. The agent must always be able to override; CLI still beats Config when the agent doesn't speak up; defaults (Production = Error, Development = Debug, --mcp-stdio = None) are unchanged.

What is this change?

DynamicLogLevelProvider is the single chokepoint for all log-level updates. The fix:

  • UpdateFromMcp no longer rejects when CLI or Config has set a level. It updates the level, flips a new IsAgentOverriding flag, and emits an Information audit log ("Log level updated to {Level} via MCP logging/setLevel (agent override).").
  • UpdateFromRuntimeConfig is guarded so a config hot-reload won't overwrite an Agent- or CLI-set level.
  • ILogLevelController gains IsAgentOverriding and updated XML docs.
  • Startup.cs wires a logger on the provider once ILoggerFactory exists, so the audit log reaches the standard pipeline.
  • McpStdioServer.HandleSetLevel enables the MCP notification writer before calling UpdateFromMcp so the audit log surfaces to the agent.

CLI > Config, default levels, and the --mcp-stdio default of None are unchanged.

How was this tested?

  • Integration Tests
  • Unit Tests
  • Manual end-to-end test against live SQL Server with the MCP Inspector

Unit tests

Added 1 new test and updated existing ones in DynamicLogLevelProviderTests to lock Agent > CLI > Config under both single-call and concurrent paths. All 9 tests pass.

Manual end-to-end test

Started the engine with --mcp-stdio --LogLevel Error and drove it from the MCP Inspector against a live SQL Server.

# What How Result
1 Agent sets level to debug Send logging/setLevel { level: "debug" } Success response ✅
2 Audit log surfaces Watch MCP notifications Info notification with override message received ✅
3 Debug logs flow despite --LogLevel Error Trigger activity (e.g. tools/list) Debug notifications received ✅

Also spot-checked HTTP mode: Config-only, CLI-over-Config, and Config-alone all behave as before.

Sample Request(s)

MCP logging/setLevel from the agent

Request:

{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "logging/setLevel",
  "params": { "level": "debug" }
}

Response:

{ "jsonrpc": "2.0", "id": 2, "result": {} }

Followed by the new visibility notification:

{
  "jsonrpc": "2.0",
  "method": "notifications/message",
  "params": {
    "level": "info",
    "logger": "Azure.DataApiBuilder.Service.Telemetry.DynamicLogLevelProvider",
    "data": "Log level updated to Debug via MCP logging/setLevel (agent override)."
  }
}

CLI usage (precedence still works as expected)

# Agent can override even when CLI pins the level:
dab start --mcp-stdio --LogLevel Error
# → agent sends logging/setLevel debug → debug logs flow

# CLI still beats Config when no agent is involved:
dab start --LogLevel Warning --config dab-config.json   # config has log-level: Information
# → only Warning and above appear

# Config alone, no CLI, no agent — unchanged:
dab start --config dab-config.json                      # config has log-level: Information
# → info: lines visible, debug: filtered out

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates Data API Builder’s dynamic logging control so that MCP agents can reliably override log levels at runtime, establishing the intended precedence order Agent (MCP) > CLI > Config > defaults, and ensuring agent-driven changes are visible through standard logging/notification pathways.

Changes:

  • Adjusts DynamicLogLevelProvider so MCP logging/setLevel can override CLI/config-set levels, and prevents hot-reload config updates from clobbering agent/CLI-selected levels.
  • Extends ILogLevelController with an IsAgentOverridden flag and updates interface documentation to reflect the new precedence rules.
  • Updates MCP stdio handling and unit tests to validate the new precedence and hot-reload behavior.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/Service/Telemetry/DynamicLogLevelProvider.cs Implements agent-overrides and guards runtime-config hot reload from overwriting agent/CLI selections; emits an audit log on agent changes.
src/Service/Startup.cs Wires an ILogger into the provider after ILoggerFactory is available so agent override audit logs flow through the normal pipeline.
src/Service.Tests/UnitTests/DynamicLogLevelProviderTests.cs Updates and adds tests to lock in Agent > CLI > Config behavior, including a hot-reload-after-agent scenario.
src/Core/Telemetry/ILogLevelController.cs Adds IsAgentOverridden and updates XML docs to match the new precedence contract.
src/Azure.DataApiBuilder.Mcp/Core/McpStdioServer.cs Reorders notification enabling to ensure the agent override audit log is forwarded to the MCP client.

Comment thread src/Service/Telemetry/DynamicLogLevelProvider.cs Outdated
Comment thread src/Azure.DataApiBuilder.Mcp/Core/McpStdioServer.cs Outdated
@anushakolan anushakolan force-pushed the dev/anushakolan/log_level_precedence_order branch from 4d15abd to eb76771 Compare May 12, 2026 21:04
@anushakolan anushakolan requested a review from Copilot May 12, 2026 21:04
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Comment thread src/Service/Telemetry/DynamicLogLevelProvider.cs
Comment thread src/Service/Startup.cs Outdated
Comment thread src/Azure.DataApiBuilder.Mcp/Core/McpStdioServer.cs
Comment thread src/Core/Telemetry/ILogLevelController.cs Outdated
Comment thread src/Service/Telemetry/DynamicLogLevelProvider.cs Outdated
Comment thread src/Service/Telemetry/DynamicLogLevelProvider.cs Outdated
Comment thread src/Service.Tests/UnitTests/DynamicLogLevelProviderTests.cs Outdated
Comment thread src/Service.Tests/UnitTests/DynamicLogLevelProviderTests.cs Outdated
Copy link
Copy Markdown
Contributor

@souvikghosh04 souvikghosh04 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Posted the comments which needs some changes. Approving this assuming those gets addressed.

Comment thread src/Service/Telemetry/DynamicLogLevelProvider.cs
Comment thread src/Service/Program.cs Outdated
Comment thread src/Service.Tests/UnitTests/DynamicLogLevelProviderTests.cs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

[Bug]: Log-level precedence should be Agent > CLI > Config (MCP logging/setLevel ignored when CLI/Config set)

4 participants