Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/mcp/coverage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,9 @@ func TestDecodeStrictBranches(t *testing.T) {
if err := decodeStrict(nil, &target); err != nil {
t.Fatalf("expected empty args decode success, got %v", err)
}
if err := decodeStrict(json.RawMessage(` null `), &target); err != nil {
t.Fatalf("expected null args decode success, got %v", err)
}
if err := decodeStrict(json.RawMessage(`{} {}`), &target); err == nil {
t.Fatalf("expected trailing JSON error")
}
Expand Down
60 changes: 51 additions & 9 deletions internal/mcp/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,67 @@ func TestHandleToolsListRegistersExpectedTools(t *testing.T) {

result := response.Result.(map[string]any)
tools := result["tools"].([]toolSpec)
assertToolOrder(t, tools)
assertStrictToolSchemas(t, tools)
byName := toolSpecsByName(tools)
assertTopDependencySchema(t, byName[toolAnalyseTop])
assertDependencySchema(t, byName[toolAnalyseDependency])
assertBaselineSchema(t, byName[toolCompareBaseline])
}

func assertToolOrder(t *testing.T, tools []toolSpec) {
t.Helper()
names := make([]string, 0, len(tools))
for _, tool := range tools {
names = append(names, tool.Name)
if tool.InputSchema["additionalProperties"] != false {
t.Fatalf("expected strict input schema for %s", tool.Name)
}
if tool.Name == toolAnalyseTop {
properties := tool.InputSchema["properties"].(map[string]any)
if _, ok := properties["dependency"]; ok {
t.Fatalf("top dependency schema should not advertise dependency input")
}
}
}
want := []string{toolAnalyseTop, toolAnalyseDependency, toolCompareBaseline, toolListLanguages}
if !slices.Equal(names, want) {
t.Fatalf("unexpected tools: %#v", names)
}
}

func assertStrictToolSchemas(t *testing.T, tools []toolSpec) {
t.Helper()
for _, tool := range tools {
if tool.InputSchema["additionalProperties"] != false {
t.Fatalf("expected strict input schema for %s", tool.Name)
}
}
}

func toolSpecsByName(tools []toolSpec) map[string]toolSpec {
byName := make(map[string]toolSpec, len(tools))
for _, tool := range tools {
byName[tool.Name] = tool
}
return byName
}

func assertTopDependencySchema(t *testing.T, tool toolSpec) {
t.Helper()
properties := tool.InputSchema["properties"].(map[string]any)
if _, ok := properties["dependency"]; ok {
t.Fatalf("top dependency schema should not advertise dependency input")
}
}

func assertDependencySchema(t *testing.T, tool toolSpec) {
t.Helper()
properties := tool.InputSchema["properties"].(map[string]any)
if _, ok := properties["topN"]; !ok {
t.Fatalf("dependency schema should advertise topN as accepted input")
}
}

func assertBaselineSchema(t *testing.T, tool toolSpec) {
t.Helper()
anyOf, ok := tool.InputSchema["anyOf"].([]map[string]any)
if !ok || len(anyOf) != 2 {
t.Fatalf("baseline schema should describe baselinePath or baselineStorePath alternatives, got %#v", tool.InputSchema["anyOf"])
}
}

func TestCallAnalyseDependencyMapsRequestAndReturnsStructuredReport(t *testing.T) {
repo := t.TempDir()
cacheEnabled := false
Expand Down
13 changes: 10 additions & 3 deletions internal/mcp/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (s *Server) tools() []toolSpec {
{
Name: toolAnalyseDependency,
Description: "Analyse one dependency in a local repository and return the report schema plus a concise summary.",
InputSchema: analysisInputSchema(true, true, false, false),
InputSchema: analysisInputSchema(true, true, true, false),
},
{
Name: toolCompareBaseline,
Expand Down Expand Up @@ -636,7 +636,7 @@ func licensePolicy(values thresholds.Values) report.LicensePolicy {
}

func decodeStrict(data json.RawMessage, target any) error {
if len(data) == 0 {
if len(data) == 0 || bytes.Equal(bytes.TrimSpace(data), []byte("null")) {
data = []byte("{}")
}
decoder := json.NewDecoder(bytes.NewReader(data))
Expand Down Expand Up @@ -819,12 +819,19 @@ func analysisInputSchema(requireDependency, allowDependency, allowTopN, requireB
properties["baselineStorePath"] = map[string]any{"type": "string", "description": "Immutable baseline snapshot directory."}
properties["baselineKey"] = map[string]any{"type": "string", "description": "Snapshot key to load from baselineStorePath."}
}
return map[string]any{
schema := map[string]any{
"type": "object",
"properties": properties,
"required": required,
"additionalProperties": false,
}
if requireBaseline {
schema["anyOf"] = []map[string]any{
{"required": []string{"baselinePath"}},
{"required": []string{"baselineStorePath", "baselineKey"}},
}
}
return schema
}

func commonAnalysisProperties() map[string]any {
Expand Down
Loading