Skip to content

Add jqschema middleware for tool call response transformation#295

Merged
lpcox merged 9 commits into
mainfrom
copilot/import-jgschema-middleware
Jan 18, 2026
Merged

Add jqschema middleware for tool call response transformation#295
lpcox merged 9 commits into
mainfrom
copilot/import-jgschema-middleware

Conversation

Copilot AI commented Jan 16, 2026

Copy link
Copy Markdown
Contributor

Implementation Complete: jqschema Tool Call Middleware ✅

Successfully implemented the jqschema shared agentic workflow from githubnext/gh-aw as a tool call middleware for the MCP Gateway.

Recent Updates

  • Fixed middleware wrapping: wrappedHandler now calls finalHandler to properly apply middleware
  • Fixed formatting issues in health.go
  • Fixed linting error: removed unnecessary int to int conversion in test (d44f1cf)
  • Documented future configuration options for selective middleware mounting

Implementation Summary

  • Middleware Package (internal/middleware/)

    • jqschema.go - Core middleware implementation
    • jqschema_test.go - Unit tests
    • jqschema_integration_test.go - Integration tests
    • README.md - Comprehensive documentation with future enhancement notes
    • demo.sh - Interactive demo script
  • Key Features

    • ✅ Uses github.com/itchyny/gojq (pure Go, no external processes)
    • ✅ Generates random 32-character hex IDs for each query
    • ✅ Stores payloads in /tmp/gh-awmg/tools-calls/{randomID}/payload.json
    • ✅ Returns first 500 chars + jq inferred schema
    • ✅ Automatically applied to all backend tools (excludes sys___*)
    • Properly integrated - middleware now correctly wraps tool handlers
  • Configuration

    • ✅ Current: ShouldApplyMiddleware function for basic filtering
    • 📝 Future: Configuration system for selective mounting, custom settings per backend
  • Testing & Quality

    • ✅ 14 unit tests (100% pass rate)
    • ✅ 3 integration tests (100% pass rate)
    • ✅ All existing tests still pass
    • ✅ Linting passes (0 issues) ✅
    • ✅ Formatting passes ✅

Response Transformation Example

Before (Original):

{
  "total_count": 1000,
  "items": [{"login": "user1", "id": 123, "verified": true}]
}

After (Transformed):

{
  "queryID": "a1b2c3d4e5f6789...",
  "payloadPath": "/tmp/gh-awmg/tools-calls/a1b2c3d4e5f6789.../payload.json",
  "preview": "{\"total_count\":1000,\"items\":[{\"login\":...",
  "schema": {
    "total_count": "number",
    "items": [{"login": "string", "id": "number", "verified": "boolean"}]
  },
  "originalSize": 234,
  "truncated": false
}

Benefits

  1. Reduced Token Usage: Schema is much smaller than full data
  2. Better Understanding: See structure without verbose output
  3. Audit Trail: Complete payloads saved for inspection
  4. Easy Debugging: Query IDs for tracking and correlation
  5. Performance: Pure Go implementation (no external jq process)
Original prompt

Import the logic found in the jgschema shared agentic workflow in githubnext/gh-aw as a tool call middleware.

  • use go module for jq instead of spawning a process
  • use random id for each query rewritten
  • store payloads in /tmp/gh-awmg/tools-calls/RND

The rewritten returned answer should be the first 500 char of the payload + jq inferred schema.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 4 commits January 16, 2026 14:11
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 jgschema logic as tool call middleware Add jqschema middleware for tool call response transformation Jan 16, 2026
Copilot AI requested a review from pelikhan January 16, 2026 14:19
@pelikhan

Copy link
Copy Markdown
Collaborator

@lpcox

we should have a story to selectively mount middlewares and configure them

@lpcox

lpcox commented Jan 18, 2026

Copy link
Copy Markdown
Collaborator

@copilot please fix these linting errors

Running [/home/runner/golangci-lint-2.8.0-linux-amd64/golangci-lint run --timeout=5m] in [/home/runner/work/gh-aw-mcpg/gh-aw-mcpg] ...
Error: internal/middleware/jqschema_integration_test.go:149:21: unnecessary conversion (unconvert)
originalSize := int(dataMap["originalSize"].(int))
^
1 issues:

  • unconvert: 1

Error: issues found

Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>

Copilot AI commented Jan 18, 2026

Copy link
Copy Markdown
Contributor Author

@copilot please fix these linting errors

Running [/home/runner/golangci-lint-2.8.0-linux-amd64/golangci-lint run --timeout=5m] in [/home/runner/work/gh-aw-mcpg/gh-aw-mcpg] ...
...

Fixed in d44f1cf. Removed the unnecessary int() conversion on line 149.

Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
Copilot AI requested a review from lpcox January 18, 2026 15:57
@lpcox

lpcox commented Jan 18, 2026

Copy link
Copy Markdown
Collaborator

@copilot around line 290 in internal/server/unified.go make sure that the wrappedhandler wraps the finalhandler rather than just the handler

Also, please fix these linting errors

make lint
shell: /usr/bin/bash -e {0}
env:
GOTOOLCHAIN: local
Running linters...
go: downloading github.com/stretchr/testify v1.11.1
go: downloading github.com/BurntSushi/toml v1.5.0
go: downloading github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
go: downloading github.com/spf13/cobra v1.10.2
go: downloading github.com/modelcontextprotocol/go-sdk v1.1.0
go: downloading github.com/itchyny/gojq v0.12.18
go: downloading golang.org/x/term v0.38.0
go: downloading github.com/davecgh/go-spew v1.1.1
go: downloading github.com/pmezard/go-difflib v1.0.0
go: downloading github.com/spf13/pflag v1.0.9
go: downloading golang.org/x/sys v0.39.0
go: downloading github.com/itchyny/timefmt-go v0.1.7
go: downloading gopkg.in/yaml.v3 v3.0.1
go: downloading github.com/inconshreveable/mousetrap v1.1.0
go: downloading gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405
go: downloading github.com/yosida95/uritemplate/v3 v3.0.2
go: downloading github.com/google/jsonschema-go v0.3.0
go: downloading github.com/google/go-cmp v0.7.0
go: downloading golang.org/x/tools v0.34.0
go: downloading golang.org/x/oauth2 v0.30.0
Running gofmt check...
The following files are not formatted:
internal/server/health.go

- Changed wrappedHandler to call finalHandler instead of handler to properly apply middleware
- Fixed gofmt formatting in health.go (removed trailing spaces)

Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>

Copilot AI commented Jan 18, 2026

Copy link
Copy Markdown
Contributor Author

@copilot around line 290 in internal/server/unified.go make sure that the wrappedhandler wraps the finalhandler rather than just the handler

Also, please fix these linting errors
...

Fixed in 8dcfdcd:

  • Line 296: Changed wrappedHandler to call finalHandler instead of handler to properly apply middleware
  • Fixed gofmt formatting in health.go (removed trailing spaces)

All linting now passes with 0 issues.

@lpcox lpcox marked this pull request as ready for review January 18, 2026 16:13
@lpcox lpcox merged commit 8fc5cd9 into main Jan 18, 2026
3 checks passed
@lpcox lpcox deleted the copilot/import-jgschema-middleware branch January 18, 2026 16:13
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