Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

【架构优化】拆分 mcpserver.ToolInputSchema 的契约路由查找与 query 参数收集并删除转发函数 (#285)

fx-kurumibcfda666abce
diff --git a/internal/mcpserver/schema.go b/internal/mcpserver/schema.go
index 31fad24b..d1bf9118 100644
--- a/internal/mcpserver/schema.go
+++ b/internal/mcpserver/schema.go
@@ -60,39 +60,8 @@ func ToolInputSchema(op application.Operation) map[string]any {
required = append(required, "path")
}
properties["query"] = map[string]any{"type": "object", "additionalProperties": map[string]any{"type": "string"}}
- // OpenAPI sometimes names a path parameter issueId where the manifest uses id.
- paths := contractObject(contract["paths"])
- var method map[string]any
- var route map[string]any
- for path, value := range paths {
- if normalizeContractPath(path) == normalizeContractPath(op.Path) {
- route = contractObject(value)
- method = contractObject(route[strings.ToLower(op.Method)])
- break
- }
- }
- queryFields := map[string]any{}
- queryRequired := []string{}
- routeParameters, _ := route["parameters"].([]any)
- methodParameters, _ := method["parameters"].([]any)
- for _, raw := range append(routeParameters, methodParameters...) {
- param := contractObject(resolveContract(raw, 0))
- if param["in"] != "query" {
- continue
- }
- name, _ := param["name"].(string)
- if name == "" {
- continue
- }
- field := map[string]any{"type": "string"}
- if description, ok := param["description"].(string); ok {
- field["description"] = description
- }
- queryFields[name] = field
- if needed, _ := param["required"].(bool); needed {
- queryRequired = append(queryRequired, name)
- }
- }
+ route, method := contractOperation(op)
+ queryFields, queryRequired := queryParameterSchema(route, method)
if len(queryFields) > 0 {
query := contractObject(properties["query"])
query["properties"] = queryFields
@@ -120,6 +89,49 @@ func ToolInputSchema(op application.Operation) map[string]any {
}
return schema
}
+
+// contractOperation finds the OpenAPI route and method objects for op.
+// OpenAPI sometimes names a path parameter issueId where the manifest uses id,
+// so paths are compared with their parameter names erased.
+func contractOperation(op application.Operation) (route, method map[string]any) {
+ for path, value := range contractObject(contract["paths"]) {
+ if normalizeContractPath(path) == normalizeContractPath(op.Path) {
+ route = contractObject(value)
+ method = contractObject(route[strings.ToLower(op.Method)])
+ break
+ }
+ }
+ return route, method
+}
+
+// queryParameterSchema collects the documented query parameters from the
+// route-level and method-level parameter lists.
+func queryParameterSchema(route, method map[string]any) (fields map[string]any, required []string) {
+ fields = map[string]any{}
+ required = []string{}
+ routeParameters, _ := route["parameters"].([]any)
+ methodParameters, _ := method["parameters"].([]any)
+ for _, raw := range append(routeParameters, methodParameters...) {
+ param := contractObject(resolveContract(raw, 0))
+ if param["in"] != "query" {
+ continue
+ }
+ name, _ := param["name"].(string)
+ if name == "" {
+ continue
+ }
+ field := map[string]any{"type": "string"}
+ if description, ok := param["description"].(string); ok {
+ field["description"] = description
+ }
+ fields[name] = field
+ if needed, _ := param["required"].(bool); needed {
+ required = append(required, name)
+ }
+ }
+ return fields, required
+}
+
func normalizeContractPath(path string) string {
parts := strings.Split(path, "/")
for i, p := range parts {
diff --git a/internal/mcpserver/schema_helpers_test.go b/internal/mcpserver/schema_helpers_test.go
new file mode 100644
index 00000000..05c75df9
--- /dev/null
+++ b/internal/mcpserver/schema_helpers_test.go
@@ -0,0 +1,111 @@
+package mcpserver
+
+import (
+ "agentflow/internal/application"
+ "reflect"
+ "testing"
+)
+
+func TestContractOperationMatchesRenamedPathParameter(t *testing.T) {
+ ToolInputSchema(application.Operation{}) // load the contract
+ route, method := contractOperation(application.Operation{Method: "GET", Path: "/api/assistant/tasks"})
+ if route == nil || method == nil {
+ t.Fatalf("documented route not found: route=%v method=%v", route != nil, method != nil)
+ }
+ if got := method["operationId"]; got != "listAssistantTasks" {
+ t.Fatalf("operationId = %v", got)
+ }
+ route, method = contractOperation(application.Operation{Method: "GET", Path: "/api/no-such-route/{id}"})
+ if route != nil || method != nil {
+ t.Fatalf("undocumented route matched: %v %v", route, method)
+ }
+}
+
+func TestQueryParameterSchemaMergesRouteAndMethodParameters(t *testing.T) {
+ route := map[string]any{"parameters": []any{
+ map[string]any{"name": "path", "in": "query", "required": true},
+ map[string]any{"name": "id", "in": "path", "required": true},
+ }}
+ method := map[string]any{"parameters": []any{
+ map[string]any{"name": "limit", "in": "query", "description": "page size"},
+ map[string]any{"name": "", "in": "query", "required": true},
+ }}
+ fields, required := queryParameterSchema(route, method)
+ want := map[string]any{
+ "path": map[string]any{"type": "string"},
+ "limit": map[string]any{"type": "string", "description": "page size"},
+ }
+ if !reflect.DeepEqual(fields, want) {
+ t.Fatalf("fields = %#v", fields)
+ }
+ if !reflect.DeepEqual(required, []string{"path"}) {
+ t.Fatalf("required = %#v", required)
+ }
+}
+
+func TestQueryParameterSchemaWithoutContract(t *testing.T) {
+ fields, required := queryParameterSchema(nil, nil)
+ if len(fields) != 0 || len(required) != 0 {
+ t.Fatalf("fields=%v required=%v", fields, required)
+ }
+}
+
+func TestContractOperationIgnoresPathParameterNames(t *testing.T) {
+ ToolInputSchema(application.Operation{}) // load the contract
+ // The contract documents /api/issues/{issueId}; manifests may use {id}.
+ route, method := contractOperation(application.Operation{Method: "GET", Path: "/api/issues/{id}"})
+ if route == nil || method == nil {
+ t.Fatalf("renamed path parameter not matched: route=%v method=%v", route != nil, method != nil)
+ }
+ if got := method["operationId"]; got != "getIssue" {
+ t.Fatalf("operationId = %v", got)
+ }
+ if _, ok := route["parameters"].([]any); !ok {
+ t.Fatalf("route-level parameters missing: %#v", route["parameters"])
+ }
+}
+
+func TestContractOperationUndocumentedMethod(t *testing.T) {
+ ToolInputSchema(application.Operation{}) // load the contract
+ // The route exists but has no PATCH operation: keep the route, drop the method.
+ route, method := contractOperation(application.Operation{Method: "PATCH", Path: "/api/issues/{id}"})
+ if route == nil {
+ t.Fatal("documented route not found")
+ }
+ if method != nil {
+ t.Fatalf("undocumented method matched: %#v", method)
+ }
+}
+
+func TestQueryParameterSchemaResolvesContractRefs(t *testing.T) {
+ ToolInputSchema(application.Operation{}) // load the contract
+ // deleteRepository declares its required id query parameter through $ref.
+ route, method := contractOperation(application.Operation{Method: "DELETE", Path: "/api/repositories"})
+ fields, required := queryParameterSchema(route, method)
+ if !reflect.DeepEqual(fields, map[string]any{"id": map[string]any{"type": "string"}}) {
+ t.Fatalf("fields = %#v", fields)
+ }
+ if !reflect.DeepEqual(required, []string{"id"}) {
+ t.Fatalf("required = %#v", required)
+ }
+ // Path parameters referenced at the route level must not leak into query.
+ route, method = contractOperation(application.Operation{Method: "GET", Path: "/api/issues/{id}/runs"})
+ fields, required = queryParameterSchema(route, method)
+ if len(fields) != 0 || len(required) != 0 {
+ t.Fatalf("path parameter leaked into query: fields=%v required=%v", fields, required)
+ }
+}
+
+func TestToolInputSchemaRequiresRefQueryParameter(t *testing.T) {
+ op := application.Operation{Method: "DELETE", Path: "/api/repositories"}
+ schema := ToolInputSchema(op)
+ if !reflect.DeepEqual(schema["required"], []string{"query"}) {
+ t.Fatalf("required = %#v", schema["required"])
+ }
+ if err := ValidateToolInput(op, map[string]any{}); err == nil {
+ t.Fatal("missing required query parameter accepted")
+ }
+ if err := ValidateToolInput(op, map[string]any{"query": map[string]any{"id": "repo-1"}}); err != nil {
+ t.Fatalf("valid input rejected: %v", err)
+ }
+}
diff --git a/internal/mcpserver/server.go b/internal/mcpserver/server.go
index 2a7a8c6c..974e4487 100644
--- a/internal/mcpserver/server.go
+++ b/internal/mcpserver/server.go
@@ -332,7 +332,7 @@ func (s *Server) toolList() []map[string]any {
tools = append(tools, map[string]any{
"name": op.Name,
"description": op.Description + " (" + op.Method + " " + op.Path + ")",
- "inputSchema": toolInputSchema(op),
+ "inputSchema": ToolInputSchema(op),
"annotations": map[string]any{
"readOnlyHint": op.ReadOnly,
"destructiveHint": !op.ReadOnly && (op.Method == "DELETE" || strings.Contains(op.Name, "reject") || strings.Contains(op.Name, "cancel")),
@@ -343,8 +343,6 @@ func (s *Server) toolList() []map[string]any {
return tools
}
-func toolInputSchema(op application.Operation) map[string]any { return ToolInputSchema(op) }
-
func (s *Server) callTool(ctx context.Context, raw json.RawMessage) (map[string]any, error) {
var params struct {
Name string `json:"name"`