-
Notifications
You must be signed in to change notification settings - Fork 38
feat: Add MCP parser plugin and body access infrastructure #361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b830576
feat: Add MCP parser plugin and body access infrastructure
huang195 4e19600
fix: Add body size limit (1MB) to forward and reverse proxy listeners
huang195 edea063
docs: Add MCP parser plugin demo and verification guide
huang195 64ea925
fix: Address PR #361 review comments
huang195 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| package plugins | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "log/slog" | ||
|
|
||
| "github.com/kagenti/kagenti-extensions/authbridge/authlib/pipeline" | ||
| ) | ||
|
|
||
| // MCPParser parses MCP JSON-RPC 2.0 request bodies and populates | ||
| // pctx.Extensions.MCP with the parsed method, tool name, resource URI, | ||
| // or prompt name for downstream policy plugins. | ||
| type MCPParser struct{} | ||
|
|
||
| func NewMCPParser() *MCPParser { return &MCPParser{} } | ||
|
|
||
| func (p *MCPParser) Name() string { return "mcp-parser" } | ||
|
|
||
| func (p *MCPParser) Capabilities() pipeline.PluginCapabilities { | ||
| return pipeline.PluginCapabilities{ | ||
| Writes: []string{"mcp"}, | ||
| BodyAccess: true, | ||
| } | ||
| } | ||
|
|
||
| func (p *MCPParser) OnRequest(_ context.Context, pctx *pipeline.Context) pipeline.Action { | ||
| if len(pctx.Body) == 0 { | ||
| slog.Debug("mcp-parser: no body, skipping") | ||
| return pipeline.Action{Type: pipeline.Continue} | ||
| } | ||
|
|
||
| var rpc jsonRPCRequest | ||
| if err := json.Unmarshal(pctx.Body, &rpc); err != nil { | ||
| slog.Debug("mcp-parser: body is not valid JSON-RPC", "error", err, "bodyLen", len(pctx.Body)) | ||
| return pipeline.Action{Type: pipeline.Continue} | ||
| } | ||
|
|
||
| ext := &pipeline.MCPExtension{ | ||
| Method: rpc.Method, | ||
| RPCID: rpc.ID, | ||
| } | ||
|
|
||
| switch rpc.Method { | ||
| case "tools/call": | ||
| ext.Tool = &pipeline.MCPToolMetadata{ | ||
| Name: rpc.stringParam("name"), | ||
| Args: rpc.mapParam("arguments"), | ||
| } | ||
| slog.Info("mcp-parser: parsed tools/call", "tool", ext.Tool.Name) | ||
| case "resources/read": | ||
| ext.Resource = &pipeline.MCPResourceMetadata{ | ||
| URI: rpc.stringParam("uri"), | ||
| } | ||
| slog.Info("mcp-parser: parsed resources/read", "uri", ext.Resource.URI) | ||
| case "prompts/get": | ||
| ext.Prompt = &pipeline.MCPPromptMetadata{ | ||
| Name: rpc.stringParam("name"), | ||
| Args: rpc.stringMapParam("arguments"), | ||
| } | ||
| slog.Info("mcp-parser: parsed prompts/get", "prompt", ext.Prompt.Name) | ||
| default: | ||
| slog.Debug("mcp-parser: untracked method", "method", rpc.Method) | ||
| } | ||
|
|
||
| pctx.Extensions.MCP = ext | ||
| return pipeline.Action{Type: pipeline.Continue} | ||
| } | ||
|
|
||
| func (p *MCPParser) OnResponse(_ context.Context, _ *pipeline.Context) pipeline.Action { | ||
| return pipeline.Action{Type: pipeline.Continue} | ||
| } | ||
|
|
||
| type jsonRPCRequest struct { | ||
| Method string `json:"method"` | ||
| ID any `json:"id"` | ||
| Params map[string]any `json:"params"` | ||
| } | ||
|
|
||
| func (r *jsonRPCRequest) stringParam(key string) string { | ||
| if v, ok := r.Params[key].(string); ok { | ||
| return v | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| func (r *jsonRPCRequest) mapParam(key string) map[string]any { | ||
| if v, ok := r.Params[key].(map[string]any); ok { | ||
| return v | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (r *jsonRPCRequest) stringMapParam(key string) map[string]string { | ||
| raw, ok := r.Params[key].(map[string]any) | ||
| if !ok { | ||
| return nil | ||
| } | ||
| out := make(map[string]string, len(raw)) | ||
| for k, v := range raw { | ||
| if s, ok := v.(string); ok { | ||
| out[k] = s | ||
| } | ||
| } | ||
| return out | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| package plugins | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/kagenti/kagenti-extensions/authbridge/authlib/pipeline" | ||
| ) | ||
|
|
||
| func TestMCPParser_Capabilities(t *testing.T) { | ||
| p := NewMCPParser() | ||
|
|
||
| if p.Name() != "mcp-parser" { | ||
| t.Errorf("Name() = %q, want %q", p.Name(), "mcp-parser") | ||
| } | ||
|
|
||
| caps := p.Capabilities() | ||
| if !caps.BodyAccess { | ||
| t.Error("BodyAccess should be true") | ||
| } | ||
| if len(caps.Writes) != 1 || caps.Writes[0] != "mcp" { | ||
| t.Errorf("Writes = %v, want [mcp]", caps.Writes) | ||
| } | ||
| } | ||
|
|
||
| func TestMCPParser_ToolCall(t *testing.T) { | ||
| p := NewMCPParser() | ||
| pctx := &pipeline.Context{ | ||
| Body: []byte(`{"jsonrpc":"2.0","method":"tools/call","id":1,"params":{"name":"get_weather","arguments":{"city":"NYC"}}}`), | ||
| } | ||
|
|
||
| action := p.OnRequest(context.Background(), pctx) | ||
| if action.Type != pipeline.Continue { | ||
| t.Fatalf("expected Continue, got %v", action.Type) | ||
| } | ||
| if pctx.Extensions.MCP == nil { | ||
| t.Fatal("Extensions.MCP is nil") | ||
| } | ||
| if pctx.Extensions.MCP.Method != "tools/call" { | ||
| t.Errorf("Method = %q, want %q", pctx.Extensions.MCP.Method, "tools/call") | ||
| } | ||
| if pctx.Extensions.MCP.RPCID != float64(1) { | ||
| t.Errorf("RPCID = %v, want 1", pctx.Extensions.MCP.RPCID) | ||
| } | ||
| if pctx.Extensions.MCP.Tool == nil { | ||
| t.Fatal("Tool is nil") | ||
| } | ||
| if pctx.Extensions.MCP.Tool.Name != "get_weather" { | ||
| t.Errorf("Tool.Name = %q, want %q", pctx.Extensions.MCP.Tool.Name, "get_weather") | ||
| } | ||
| if pctx.Extensions.MCP.Tool.Args["city"] != "NYC" { | ||
| t.Errorf("Tool.Args[city] = %v, want %q", pctx.Extensions.MCP.Tool.Args["city"], "NYC") | ||
| } | ||
| } | ||
|
|
||
| func TestMCPParser_ResourceRead(t *testing.T) { | ||
| p := NewMCPParser() | ||
| pctx := &pipeline.Context{ | ||
| Body: []byte(`{"jsonrpc":"2.0","method":"resources/read","id":2,"params":{"uri":"file:///tmp/data.csv"}}`), | ||
| } | ||
|
|
||
| action := p.OnRequest(context.Background(), pctx) | ||
| if action.Type != pipeline.Continue { | ||
| t.Fatalf("expected Continue, got %v", action.Type) | ||
| } | ||
| if pctx.Extensions.MCP == nil { | ||
| t.Fatal("Extensions.MCP is nil") | ||
| } | ||
| if pctx.Extensions.MCP.Resource == nil { | ||
| t.Fatal("Resource is nil") | ||
| } | ||
| if pctx.Extensions.MCP.Resource.URI != "file:///tmp/data.csv" { | ||
| t.Errorf("Resource.URI = %q, want %q", pctx.Extensions.MCP.Resource.URI, "file:///tmp/data.csv") | ||
| } | ||
| } | ||
|
|
||
| func TestMCPParser_PromptGet(t *testing.T) { | ||
| p := NewMCPParser() | ||
| pctx := &pipeline.Context{ | ||
| Body: []byte(`{"jsonrpc":"2.0","method":"prompts/get","id":3,"params":{"name":"summarize","arguments":{"style":"brief"}}}`), | ||
| } | ||
|
|
||
| action := p.OnRequest(context.Background(), pctx) | ||
| if action.Type != pipeline.Continue { | ||
| t.Fatalf("expected Continue, got %v", action.Type) | ||
| } | ||
| if pctx.Extensions.MCP == nil { | ||
| t.Fatal("Extensions.MCP is nil") | ||
| } | ||
| if pctx.Extensions.MCP.Prompt == nil { | ||
| t.Fatal("Prompt is nil") | ||
| } | ||
| if pctx.Extensions.MCP.Prompt.Name != "summarize" { | ||
| t.Errorf("Prompt.Name = %q, want %q", pctx.Extensions.MCP.Prompt.Name, "summarize") | ||
| } | ||
| if pctx.Extensions.MCP.Prompt.Args["style"] != "brief" { | ||
| t.Errorf("Prompt.Args[style] = %q, want %q", pctx.Extensions.MCP.Prompt.Args["style"], "brief") | ||
| } | ||
| } | ||
|
|
||
| func TestMCPParser_UnknownMethod(t *testing.T) { | ||
| p := NewMCPParser() | ||
| pctx := &pipeline.Context{ | ||
| Body: []byte(`{"jsonrpc":"2.0","method":"notifications/initialized","id":4}`), | ||
| } | ||
|
|
||
| action := p.OnRequest(context.Background(), pctx) | ||
| if action.Type != pipeline.Continue { | ||
| t.Fatalf("expected Continue, got %v", action.Type) | ||
| } | ||
| if pctx.Extensions.MCP == nil { | ||
| t.Fatal("Extensions.MCP is nil") | ||
| } | ||
| if pctx.Extensions.MCP.Method != "notifications/initialized" { | ||
| t.Errorf("Method = %q, want %q", pctx.Extensions.MCP.Method, "notifications/initialized") | ||
| } | ||
| if pctx.Extensions.MCP.Tool != nil { | ||
| t.Error("Tool should be nil for unknown method") | ||
| } | ||
| if pctx.Extensions.MCP.Resource != nil { | ||
| t.Error("Resource should be nil for unknown method") | ||
| } | ||
| if pctx.Extensions.MCP.Prompt != nil { | ||
| t.Error("Prompt should be nil for unknown method") | ||
| } | ||
| } | ||
|
|
||
| func TestMCPParser_NilBody(t *testing.T) { | ||
| p := NewMCPParser() | ||
| pctx := &pipeline.Context{Body: nil} | ||
|
|
||
| action := p.OnRequest(context.Background(), pctx) | ||
| if action.Type != pipeline.Continue { | ||
| t.Fatalf("expected Continue, got %v", action.Type) | ||
| } | ||
| if pctx.Extensions.MCP != nil { | ||
| t.Error("Extensions.MCP should be nil when body is nil") | ||
| } | ||
| } | ||
|
|
||
| func TestMCPParser_EmptyBody(t *testing.T) { | ||
| p := NewMCPParser() | ||
| pctx := &pipeline.Context{Body: []byte{}} | ||
|
|
||
| action := p.OnRequest(context.Background(), pctx) | ||
| if action.Type != pipeline.Continue { | ||
| t.Fatalf("expected Continue, got %v", action.Type) | ||
| } | ||
| if pctx.Extensions.MCP != nil { | ||
| t.Error("Extensions.MCP should be nil when body is empty") | ||
| } | ||
| } | ||
|
|
||
| func TestMCPParser_InvalidJSON(t *testing.T) { | ||
| p := NewMCPParser() | ||
| pctx := &pipeline.Context{Body: []byte("not json")} | ||
|
|
||
| action := p.OnRequest(context.Background(), pctx) | ||
| if action.Type != pipeline.Continue { | ||
| t.Fatalf("expected Continue, got %v", action.Type) | ||
| } | ||
| if pctx.Extensions.MCP != nil { | ||
| t.Error("Extensions.MCP should be nil for invalid JSON") | ||
| } | ||
| } | ||
|
|
||
| func TestMCPParser_MissingParams(t *testing.T) { | ||
| p := NewMCPParser() | ||
| pctx := &pipeline.Context{ | ||
| Body: []byte(`{"jsonrpc":"2.0","method":"tools/call","id":5}`), | ||
| } | ||
|
|
||
| action := p.OnRequest(context.Background(), pctx) | ||
| if action.Type != pipeline.Continue { | ||
| t.Fatalf("expected Continue, got %v", action.Type) | ||
| } | ||
| if pctx.Extensions.MCP == nil { | ||
| t.Fatal("Extensions.MCP is nil") | ||
| } | ||
| if pctx.Extensions.MCP.Tool == nil { | ||
| t.Fatal("Tool should not be nil for tools/call") | ||
| } | ||
| if pctx.Extensions.MCP.Tool.Name != "" { | ||
| t.Errorf("Tool.Name = %q, want empty", pctx.Extensions.MCP.Tool.Name) | ||
| } | ||
| } | ||
|
|
||
| func TestMCPParser_OnResponse(t *testing.T) { | ||
| p := NewMCPParser() | ||
| action := p.OnResponse(context.Background(), &pipeline.Context{}) | ||
| if action.Type != pipeline.Continue { | ||
| t.Errorf("OnResponse should return Continue, got %v", action.Type) | ||
| } | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could use a descriptive comment?