-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools_flow.go
More file actions
98 lines (93 loc) · 3.62 KB
/
tools_flow.go
File metadata and controls
98 lines (93 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Tools wiring the architecture-flow MCP tool.
//
// Single tool: generate_flow. Wraps the internal/flow engine; supports
// the five Java views (overview/ci/deploy/runtime/auth) and the four
// renderer formats (json/mermaid/dot/yaml). Mirrors Java McpTools
// .generateFlow but with two extra formats (dot, yaml) that the Java
// renderer also ships now.
package mcp
import (
"context"
"encoding/json"
"fmt"
"github.com/randomcodespace/codeiq/internal/flow"
)
// flowTools returns the slice of flow-facing Tool definitions for d.
// Today there is only one flow tool — leaving the slice plumbing in
// place keeps this file symmetric with tools_graph / tools_topology
// and makes RegisterFlow trivial to extend if drill-down views land.
func flowTools(d *Deps) []Tool {
return []Tool{toolGenerateFlow(d)}
}
// RegisterFlow appends every flow-facing tool to srv. Symmetric with
// RegisterGraph / RegisterTopology.
func RegisterFlow(srv *Server, d *Deps) error {
for _, t := range flowTools(d) {
if err := srv.Register(t); err != nil {
return fmt.Errorf("mcp: register flow tool %q: %w", t.Name, err)
}
}
return nil
}
// toolGenerateFlow builds the `generate_flow` tool. The view defaults
// to "overview" and the format defaults to "json" — matches the Java
// side defaults. Unknown views surface as INVALID_INPUT (mirrors Java
// IllegalArgumentException → errorEnvelope path) rather than internal
// errors so clients can fix the typo without retrying.
//
// When the engine is not wired (no analysis data) the response mirrors
// the Java contract — a `{ "error": "..." }` envelope rather than a
// generic INTERNAL_ERROR — so existing MCP clients that key off `error`
// keep working unchanged.
func toolGenerateFlow(d *Deps) Tool {
return Tool{
Name: "generate_flow",
Description: "Generate an architecture flow diagram for the " +
"codebase. Views: overview (full system), ci (build " +
"pipeline), deploy (deployment topology), runtime (service " +
"communication), auth (security flow). Output as JSON, " +
"Mermaid, DOT, or YAML.",
Schema: json.RawMessage(`{"type":"object","properties":{"view":{"type":"string"},"format":{"type":"string"}}}`),
Handler: func(ctx context.Context, raw json.RawMessage) (any, error) {
var p struct {
View string `json:"view"`
Format string `json:"format"`
}
_ = json.Unmarshal(raw, &p)
if p.View == "" {
p.View = "overview"
}
if p.Format == "" {
p.Format = "json"
}
if !flow.IsKnownView(p.View) {
return NewErrorEnvelope(CodeInvalidInput,
fmt.Errorf("unknown view %q (valid: overview, ci, deploy, runtime, auth)", p.View),
RequestID(ctx)), nil
}
if d.Flow == nil {
// Matches Java: "No analysis data available. Run 'codeiq
// analyze' first." Returned in the `error` legacy field
// so existing MCP clients keep their existing handling.
return map[string]string{
"error": "No analysis data available. Run 'codeiq enrich' first.",
}, nil
}
diag, err := d.Flow.Generate(ctx, flow.View(p.View))
if err != nil {
return NewErrorEnvelope(CodeInternalError, err, RequestID(ctx)), nil
}
rendered, err := flow.Render(diag, p.Format)
if err != nil {
return NewErrorEnvelope(CodeInvalidInput, err, RequestID(ctx)), nil
}
// For JSON / YAML we have a text body the client wants raw;
// for Mermaid / DOT it's already text. Mirror Java's pass-
// through: return the rendered string directly (the SDK
// wrapper takes care of converting non-map returns to a
// text-content body). Wrapping in another JSON envelope
// would double-encode JSON-shaped output.
return rendered, nil
},
}
}