feat(mcp): auto-enable JSON output for better LLM parsing

Summary

Automatically enable JSON output for glab MCP tools that support it, making responses significantly more LLM-friendly.

Motivation

JSON output provides several advantages for LLM consumption:

  • Easier parsing: LLMs can reliably extract specific fields (IDs, titles, labels, etc.) from structured data
  • Better for follow-up actions: LLMs can easily extract an issue ID to pass to another command
  • Proper MCP usage: Enables correct use of the structuredContent field for typed tool outputs
  • More reliable: No need to parse formatted tables with varying column widths

Implementation

Auto-detection

  • Checks if the command has an --output flag
  • If present AND user hasn't specified output format, auto-adds --output json
  • User-specified output formats are always respected

Response Handling

// Try to parse as JSON
if err := json.Unmarshal([]byte(processedOutput), &structuredData); err == nil {
    return &mcp.CallToolResult{
        Content: []mcp.Content{
            &mcp.TextContent{Text: processedOutput}, // JSON string
        },
        StructuredContent: structuredData, // Parsed for LLM
    }, nil
}
// Fallback to plain text if not JSON

MCP Spec Compliance

Follows the recommendation: "For backwards compatibility, a tool that returns structured content SHOULD also return the serialized JSON in a TextContent block"

Testing

  • All unit tests pass (updated expectations for auto-added flag)
  • Build succeeds
  • Fallback to plain text for commands without JSON support works
  • User-specified output formats are respected

Examples

Before (table output):

ID    Title           Labels
#123  Fix bug         bug, urgent
#124  Add feature     enhancement

After (JSON + structured):

[
  {"id": 123, "title": "Fix bug", "labels": ["bug", "urgent"]},
  {"id": 124, "title": "Add feature", "labels": ["enhancement"]}
]

Plus the parsed JSON in structuredContent for direct LLM access.

Builds on !2861 (merged) (content-only fix)

Merge request reports

Loading