Add token-based output guardrail to MCP server logs command with configurable limit#2616
Merged
Merged
Conversation
- Implement size checking with 100KB threshold - Generate JSON schema and jq suggestions when limit exceeded - Add comprehensive unit tests for guardrail functionality - Update logs tool description to document the guardrail Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add garderail to mcp-server logs command output
Add 100KB output size guardrail to MCP server logs command
Oct 27, 2025
Collaborator
|
@copilot limit to 10 KB |
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Contributor
Author
Copilot
AI
changed the title
Add 100KB output size guardrail to MCP server logs command
Add 10KB output size guardrail to MCP server logs command
Oct 27, 2025
pelikhan
approved these changes
Oct 27, 2025
Contributor
|
Agentic Changeset Generator triggered by this pull request. |
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR adds a 10KB output size guardrail to the MCP server's logs command to prevent overwhelming AI models and clients with massive JSON payloads. When output exceeds 10KB, the command returns a structured response containing the data schema and suggested jq filter queries instead of the full output.
Key changes:
- Implements automatic size checking that intercepts outputs >10KB
- Returns schema description and 8 common jq filter patterns when guardrail triggers
- Updates MCP server tool description to document the 10KB limit
Reviewed Changes
Copilot reviewed 6 out of 7 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/cli/mcp_logs_guardrail.go | Core guardrail implementation with size checking, schema generation, and jq query suggestions |
| pkg/cli/mcp_server.go | Integration of guardrail into MCP logs tool handler and updated tool description |
| pkg/cli/mcp_logs_guardrail_test.go | Unit tests covering size thresholds, schema generation, and query suggestions |
| pkg/cli/mcp_logs_guardrail_integration_test.go | Integration tests verifying guardrail behavior in MCP server context |
| MCP_LOGS_GUARDRAIL.md | Documentation describing the guardrail feature, usage examples, and implementation details |
| .github/workflows/go.mod | Formatting cleanup (reordering lines) |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Collaborator
|
@copilot use tokens for the limit default 12000, optional argument to change |
…ult and configurable max_tokens parameter Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Contributor
Author
Copilot
AI
changed the title
Add 10KB output size guardrail to MCP server logs command
Add token-based output guardrail to MCP server logs command with configurable limit
Oct 27, 2025
pelikhan
approved these changes
Oct 27, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
✅ Add Token-Based Output Guardrail to MCP Server Logs Command - COMPLETE
Goal: Ensure the MCP server "logs" command has a guardrail to prevent massive output by automatically suggesting a schema+jq approach when output is too large.
Summary
This PR implements a smart token-based guardrail for the MCP server's
logscommand that prevents overwhelming output while providing helpful guidance to users. When the JSON output exceeds the token limit (default: 12000 tokens, configurable), the command automatically returns a structured response containing:Key Features
max_tokensparameter allows customization per requestImplementation Details
Files Added:
pkg/cli/mcp_logs_guardrail.go- Core implementation with token estimationpkg/cli/mcp_logs_guardrail_test.go- Unit tests (11 tests including custom limit tests)pkg/cli/mcp_logs_guardrail_integration_test.go- Integration testspecs/MCP_LOGS_GUARDRAIL.md- Complete documentationFiles Modified:
pkg/cli/mcp_server.go- Added guardrail check and max_tokens parameter to logs tool.gitignore- Exclude manual test scriptsToken Estimation: Uses approximately 4 characters per token (OpenAI's rule of thumb) to estimate token count from text output.
Example Guardrail Response
{ "message": "⚠️ Output size (15000 tokens) exceeds the limit (12000 tokens). To reduce output size, use the 'jq' parameter with one of the suggested queries below.", "output_tokens": 15000, "output_size_limit": 12000, "schema": { "description": "Complete structured data for workflow logs", "type": "object", "fields": { "summary": {"type": "object", "description": "Aggregate metrics..."}, "runs": {"type": "array", "description": "Array of workflow run data..."}, ... } }, "suggested_queries": [ {"description": "Get only the summary statistics", "query": ".summary"}, {"description": "Get first 5 runs", "query": ".runs[:5]"}, {"description": "Get only failed runs", "query": ".runs | map(select(.conclusion == \"failure\"))"}, ...8 total queries ] }Configuring the Token Limit
Users can customize the token limit using the
max_tokensparameter:{ "name": "logs", "arguments": { "count": 100, "max_tokens": 20000 } }Testing
✅ All Tests Passing:
Suggested jq Queries
.summary- Summary statistics only.runs | map({database_id, workflow_name, status})- Run IDs and basic info.runs | map(select(.conclusion == "failure"))- Failed runs only{summary, runs: .runs[:5]}- Summary + first 5 runs{errors_and_warnings, missing_tools, mcp_failures}- Diagnostics only.tool_usage- Tool usage statistics.runs | map(select(.token_usage > 10000))- High token usage.runs | map(select(.workflow_name == "NAME"))- Specific workflowDocumentation
Complete documentation available in
specs/MCP_LOGS_GUARDRAIL.mdincluding:Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.