@ai-content-space/loopx 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/README.md +422 -57
  2. package/README.zh-CN.md +485 -0
  3. package/assets/logo.svg +89 -0
  4. package/package.json +5 -1
  5. package/plugins/loopx/.codex-plugin/plugin.json +1 -1
  6. package/plugins/loopx/scripts/plugin-install.test.mjs +14 -0
  7. package/plugins/loopx/skills/archive/SKILL.md +49 -0
  8. package/plugins/loopx/skills/build/SKILL.md +111 -9
  9. package/plugins/loopx/skills/clarify/SKILL.md +129 -8
  10. package/plugins/loopx/skills/debug/SKILL.md +296 -0
  11. package/plugins/loopx/skills/debug/condition-based-waiting.md +115 -0
  12. package/plugins/loopx/skills/debug/defense-in-depth.md +122 -0
  13. package/plugins/loopx/skills/debug/find-polluter.sh +63 -0
  14. package/plugins/loopx/skills/debug/root-cause-tracing.md +169 -0
  15. package/plugins/loopx/skills/go-style/SKILL.md +71 -0
  16. package/plugins/loopx/skills/kratos/SKILL.md +74 -0
  17. package/plugins/loopx/skills/kratos/references/advanced-features.md +314 -0
  18. package/plugins/loopx/skills/kratos/references/architecture.md +488 -0
  19. package/plugins/loopx/skills/kratos/references/configuration.md +399 -0
  20. package/plugins/loopx/skills/kratos/references/http-customization.md +512 -0
  21. package/plugins/loopx/skills/kratos/references/middleware-logging.md +400 -0
  22. package/plugins/loopx/skills/kratos/references/proto-api-design.md +432 -0
  23. package/plugins/loopx/skills/kratos/references/security-auth.md +411 -0
  24. package/plugins/loopx/skills/kratos/references/troubleshooting.md +385 -0
  25. package/plugins/loopx/skills/plan/SKILL.md +24 -3
  26. package/plugins/loopx/skills/review/SKILL.md +98 -1
  27. package/plugins/loopx/skills/tdd/SKILL.md +371 -0
  28. package/plugins/loopx/skills/tdd/testing-anti-patterns.md +299 -0
  29. package/plugins/loopx/skills/verify/SKILL.md +139 -0
  30. package/scripts/codex-stop-hook.mjs +71 -0
  31. package/scripts/codex-workflow-hook.mjs +248 -0
  32. package/skills/archive/SKILL.md +49 -0
  33. package/skills/build/SKILL.md +111 -9
  34. package/skills/clarify/SKILL.md +129 -8
  35. package/skills/debug/SKILL.md +296 -0
  36. package/skills/debug/condition-based-waiting.md +115 -0
  37. package/skills/debug/defense-in-depth.md +122 -0
  38. package/skills/debug/find-polluter.sh +63 -0
  39. package/skills/debug/root-cause-tracing.md +169 -0
  40. package/skills/go-style/SKILL.md +71 -0
  41. package/skills/kratos/SKILL.md +74 -0
  42. package/skills/kratos/references/advanced-features.md +314 -0
  43. package/skills/kratos/references/architecture.md +488 -0
  44. package/skills/kratos/references/configuration.md +399 -0
  45. package/skills/kratos/references/http-customization.md +512 -0
  46. package/skills/kratos/references/middleware-logging.md +400 -0
  47. package/skills/kratos/references/proto-api-design.md +432 -0
  48. package/skills/kratos/references/security-auth.md +411 -0
  49. package/skills/kratos/references/troubleshooting.md +385 -0
  50. package/skills/plan/SKILL.md +20 -3
  51. package/skills/review/SKILL.md +98 -1
  52. package/skills/tdd/SKILL.md +371 -0
  53. package/skills/tdd/testing-anti-patterns.md +299 -0
  54. package/skills/verify/SKILL.md +139 -0
  55. package/src/build-runtime.mjs +311 -26
  56. package/src/build-stop-gate.mjs +94 -0
  57. package/src/cli.mjs +57 -5
  58. package/src/codex-exec-runtime.mjs +105 -5
  59. package/src/context-manifest.mjs +172 -0
  60. package/src/html-views.mjs +316 -0
  61. package/src/install-discovery.mjs +352 -5
  62. package/src/next-skill.mjs +57 -5
  63. package/src/plan-runtime.mjs +102 -122
  64. package/src/review-runtime.mjs +558 -0
  65. package/src/runtime-maintenance.mjs +429 -14
  66. package/src/template-governance.mjs +223 -0
  67. package/src/workflow.mjs +2341 -120
  68. package/src/workspace-context.mjs +166 -0
  69. package/src/workspace-memory.mjs +69 -0
@@ -0,0 +1,169 @@
1
+ # Root Cause Tracing
2
+
3
+ ## Overview
4
+
5
+ Bugs often manifest deep in the call stack (git init in wrong directory, file created in wrong location, database opened with wrong path). Your instinct is to fix where the error appears, but that's treating a symptom.
6
+
7
+ **Core principle:** Trace backward through the call chain until you find the original trigger, then fix at the source.
8
+
9
+ ## When to Use
10
+
11
+ ```dot
12
+ digraph when_to_use {
13
+ "Bug appears deep in stack?" [shape=diamond];
14
+ "Can trace backwards?" [shape=diamond];
15
+ "Fix at symptom point" [shape=box];
16
+ "Trace to original trigger" [shape=box];
17
+ "BETTER: Also add defense-in-depth" [shape=box];
18
+
19
+ "Bug appears deep in stack?" -> "Can trace backwards?" [label="yes"];
20
+ "Can trace backwards?" -> "Trace to original trigger" [label="yes"];
21
+ "Can trace backwards?" -> "Fix at symptom point" [label="no - dead end"];
22
+ "Trace to original trigger" -> "BETTER: Also add defense-in-depth";
23
+ }
24
+ ```
25
+
26
+ **Use when:**
27
+ - Error happens deep in execution (not at entry point)
28
+ - Stack trace shows long call chain
29
+ - Unclear where invalid data originated
30
+ - Need to find which test/code triggers the problem
31
+
32
+ ## The Tracing Process
33
+
34
+ ### 1. Observe the Symptom
35
+ ```
36
+ Error: git init failed in ~/project/packages/core
37
+ ```
38
+
39
+ ### 2. Find Immediate Cause
40
+ **What code directly causes this?**
41
+ ```typescript
42
+ await execFileAsync('git', ['init'], { cwd: projectDir });
43
+ ```
44
+
45
+ ### 3. Ask: What Called This?
46
+ ```typescript
47
+ WorktreeManager.createSessionWorktree(projectDir, sessionId)
48
+ → called by Session.initializeWorkspace()
49
+ → called by Session.create()
50
+ → called by test at Project.create()
51
+ ```
52
+
53
+ ### 4. Keep Tracing Up
54
+ **What value was passed?**
55
+ - `projectDir = ''` (empty string!)
56
+ - Empty string as `cwd` resolves to `process.cwd()`
57
+ - That's the source code directory!
58
+
59
+ ### 5. Find Original Trigger
60
+ **Where did empty string come from?**
61
+ ```typescript
62
+ const context = setupCoreTest(); // Returns { tempDir: '' }
63
+ Project.create('name', context.tempDir); // Accessed before beforeEach!
64
+ ```
65
+
66
+ ## Adding Stack Traces
67
+
68
+ When you can't trace manually, add instrumentation:
69
+
70
+ ```typescript
71
+ // Before the problematic operation
72
+ async function gitInit(directory: string) {
73
+ const stack = new Error().stack;
74
+ console.error('DEBUG git init:', {
75
+ directory,
76
+ cwd: process.cwd(),
77
+ nodeEnv: process.env.NODE_ENV,
78
+ stack,
79
+ });
80
+
81
+ await execFileAsync('git', ['init'], { cwd: directory });
82
+ }
83
+ ```
84
+
85
+ **Critical:** Use `console.error()` in tests (not logger - may not show)
86
+
87
+ **Run and capture:**
88
+ ```bash
89
+ npm test 2>&1 | grep 'DEBUG git init'
90
+ ```
91
+
92
+ **Analyze stack traces:**
93
+ - Look for test file names
94
+ - Find the line number triggering the call
95
+ - Identify the pattern (same test? same parameter?)
96
+
97
+ ## Finding Which Test Causes Pollution
98
+
99
+ If something appears during tests but you don't know which test:
100
+
101
+ Use the bisection script `find-polluter.sh` in this directory:
102
+
103
+ ```bash
104
+ ./find-polluter.sh '.git' 'src/**/*.test.ts'
105
+ ```
106
+
107
+ Runs tests one-by-one, stops at first polluter. See script for usage.
108
+
109
+ ## Real Example: Empty projectDir
110
+
111
+ **Symptom:** `.git` created in `packages/core/` (source code)
112
+
113
+ **Trace chain:**
114
+ 1. `git init` runs in `process.cwd()` ← empty cwd parameter
115
+ 2. WorktreeManager called with empty projectDir
116
+ 3. Session.create() passed empty string
117
+ 4. Test accessed `context.tempDir` before beforeEach
118
+ 5. setupCoreTest() returns `{ tempDir: '' }` initially
119
+
120
+ **Root cause:** Top-level variable initialization accessing empty value
121
+
122
+ **Fix:** Made tempDir a getter that throws if accessed before beforeEach
123
+
124
+ **Also added defense-in-depth:**
125
+ - Layer 1: Project.create() validates directory
126
+ - Layer 2: WorkspaceManager validates not empty
127
+ - Layer 3: NODE_ENV guard refuses git init outside tmpdir
128
+ - Layer 4: Stack trace logging before git init
129
+
130
+ ## Key Principle
131
+
132
+ ```dot
133
+ digraph principle {
134
+ "Found immediate cause" [shape=ellipse];
135
+ "Can trace one level up?" [shape=diamond];
136
+ "Trace backwards" [shape=box];
137
+ "Is this the source?" [shape=diamond];
138
+ "Fix at source" [shape=box];
139
+ "Add validation at each layer" [shape=box];
140
+ "Bug impossible" [shape=doublecircle];
141
+ "NEVER fix just the symptom" [shape=octagon, style=filled, fillcolor=red, fontcolor=white];
142
+
143
+ "Found immediate cause" -> "Can trace one level up?";
144
+ "Can trace one level up?" -> "Trace backwards" [label="yes"];
145
+ "Can trace one level up?" -> "NEVER fix just the symptom" [label="no"];
146
+ "Trace backwards" -> "Is this the source?";
147
+ "Is this the source?" -> "Trace backwards" [label="no - keeps going"];
148
+ "Is this the source?" -> "Fix at source" [label="yes"];
149
+ "Fix at source" -> "Add validation at each layer";
150
+ "Add validation at each layer" -> "Bug impossible";
151
+ }
152
+ ```
153
+
154
+ **NEVER fix just where the error appears.** Trace back to find the original trigger.
155
+
156
+ ## Stack Trace Tips
157
+
158
+ **In tests:** Use `console.error()` not logger - logger may be suppressed
159
+ **Before operation:** Log before the dangerous operation, not after it fails
160
+ **Include context:** Directory, cwd, environment variables, timestamps
161
+ **Capture stack:** `new Error().stack` shows complete call chain
162
+
163
+ ## Real-World Impact
164
+
165
+ From debugging session (2025-10-03):
166
+ - Found root cause through 5-level trace
167
+ - Fixed at source (getter validation)
168
+ - Added 4 layers of defense
169
+ - 1847 tests passed, zero pollution
@@ -0,0 +1,71 @@
1
+ ---
2
+ name: go-style
3
+ description: Go language style support for loopx. Use before creating or editing Go (.go) files, especially inside build execution lanes.
4
+ ---
5
+
6
+ # Go Style
7
+
8
+ ## Purpose
9
+
10
+ `go-style` is a lightweight Go coding discipline skill. It should guide edits to `.go` files without overriding the repository's established conventions.
11
+
12
+ Use it as a support skill from `build` when Go files are created or modified, and directly when the user asks for Go style, idiomatic Go, or Go code cleanup.
13
+
14
+ ## Core Rules
15
+
16
+ - Preserve local project style first. If nearby code conflicts with this skill, follow the local pattern unless it is clearly broken.
17
+ - Keep the happy path straight down. Return early for errors and guard clauses.
18
+ - Put `context.Context` first in functions that accept a context.
19
+ - Wrap propagated errors with operation context using `%w` when the caller benefits from the cause.
20
+ - Do not wrap errors when returning framework/status errors that must preserve exact type or code semantics.
21
+ - Use sentinel errors, typed errors, or framework errors according to the existing project pattern; do not force sentinel errors everywhere.
22
+ - Avoid shadowing Go predeclared identifiers such as `len`, `error`, `string`, `copy`, `new`, and `make`.
23
+ - Keep interfaces small and define them at the consumer boundary when practical.
24
+ - Prefer table-driven tests for behavior matrices.
25
+ - Run `gofmt` on edited Go files before verification.
26
+
27
+ ## Error Handling
28
+
29
+ Good default:
30
+
31
+ ```go
32
+ user, err := repo.GetUser(ctx, userID)
33
+ if err != nil {
34
+ return nil, fmt.Errorf("get user %s: %w", userID, err)
35
+ }
36
+ ```
37
+
38
+ Use exact framework errors when the framework contract requires them:
39
+
40
+ ```go
41
+ if !allowed {
42
+ return nil, errors.Forbidden("PERMISSION_DENIED", "permission denied")
43
+ }
44
+ ```
45
+
46
+ Expected error categories may be represented by:
47
+
48
+ - package-level sentinel errors with `errors.Is`
49
+ - typed errors with structured fields
50
+ - Kratos/API status errors
51
+ - existing project domain error helpers
52
+
53
+ Choose the one already used in the codebase.
54
+
55
+ ## Comments
56
+
57
+ - Exported symbols should have Go doc comments that start with the symbol name.
58
+ - Short local comments are acceptable when they explain why, not what.
59
+ - Prefer complete sentences for package, exported type, exported function, and non-obvious behavior comments.
60
+
61
+ ## Verification
62
+
63
+ For Go edits, prefer the narrowest meaningful verification first, then broaden if the touched surface is shared:
64
+
65
+ ```bash
66
+ gofmt -w <edited-go-files>
67
+ go test ./...
68
+ go vet ./...
69
+ ```
70
+
71
+ Use project-specific commands when present, such as `make test`, `make lint`, `golangci-lint run`, or repository scripts.
@@ -0,0 +1,74 @@
1
+ ---
2
+ name: kratos
3
+ description: Go-Kratos framework support for loopx. Use for Kratos microservices, proto/buf API work, service/biz/data layering, Kratos middleware, auth, configuration, and troubleshooting.
4
+ ---
5
+
6
+ # Kratos
7
+
8
+ ## Purpose
9
+
10
+ `kratos` supports Go-Kratos microservice work while staying subordinate to the repository's existing architecture.
11
+
12
+ Use this skill when the user mentions Kratos, protobuf APIs, buf, service/biz/data layering, Kratos HTTP/gRPC servers, middleware, JWT/Casbin auth, or when the repository shows Kratos signals.
13
+
14
+ ## Project Detection
15
+
16
+ Before applying Kratos-specific patterns, inspect for these signals:
17
+
18
+ - `buf.yaml` or `buf.gen.yaml`
19
+ - `api/**/*.proto`
20
+ - `internal/service/`
21
+ - `internal/biz/`
22
+ - `internal/data/`
23
+ - imports containing `github.com/go-kratos/kratos/v2`
24
+
25
+ If signals are weak, ask whether to proceed with Kratos conventions before creating framework-specific structure.
26
+
27
+ ## Decision Map
28
+
29
+ Use only the reference file needed for the current task:
30
+
31
+ - New API / proto: `references/proto-api-design.md`
32
+ - Service, biz, data layering: `references/architecture.md`
33
+ - Configuration / startup: `references/configuration.md`
34
+ - HTTP response customization, WebSocket, files: `references/http-customization.md`
35
+ - JWT / Casbin / auth: `references/security-auth.md`
36
+ - Middleware / logging: `references/middleware-logging.md`
37
+ - Errors and troubleshooting: `references/troubleshooting.md`
38
+ - MCP / advanced extensions: `references/advanced-features.md`
39
+
40
+ ## Architecture Defaults
41
+
42
+ - Keep protocol concerns in `internal/service`.
43
+ - Keep business rules and use cases in `internal/biz`.
44
+ - Keep persistence, repositories, and external clients in `internal/data`.
45
+ - Avoid leaking proto types into `biz` unless the existing project already does.
46
+ - Prefer existing dependency injection style. Use `fx` examples only when the project already uses `fx`.
47
+ - Preserve generated-code boundaries; edit source `.proto` or handwritten layers, not generated files.
48
+
49
+ ## Proto/API Defaults
50
+
51
+ - Confirm package and `go_package` before adding proto files.
52
+ - Add HTTP annotations only when HTTP exposure is required.
53
+ - Use validation annotations when the project already uses `buf.validate` or `protovalidate`.
54
+ - Regenerate code with the repository's existing command, such as `buf generate`, `make api`, or project scripts.
55
+
56
+ ## Integration With Other loopx Skills
57
+
58
+ - Use `go-style` for handwritten `.go` edits.
59
+ - Use `tdd` for behavior changes when a meaningful failing test can be written before implementation.
60
+ - Use `debug` for Kratos runtime failures, generated-code mismatches, middleware ordering bugs, or request/response behavior that is not understood.
61
+ - Use `verify` before claiming the API, generated code, or service behavior is ready.
62
+
63
+ ## Verification
64
+
65
+ Prefer project-native commands. Common Kratos verification commands include:
66
+
67
+ ```bash
68
+ buf lint
69
+ buf generate
70
+ go test ./...
71
+ go vet ./...
72
+ ```
73
+
74
+ If the project uses Make targets, prefer those over ad hoc commands.
@@ -0,0 +1,314 @@
1
+ # Advanced Features
2
+
3
+ Guide for MCP server integration and other advanced Kratos features.
4
+
5
+ ## When to Use
6
+
7
+ - Integrating Kratos with AI agents via MCP
8
+ - Building tool-enabled microservices
9
+
10
+ ---
11
+
12
+ ## MCP Server Integration
13
+
14
+ Kratos can be combined with MCP (Model Context Protocol) to expose microservice functionality as AI agent tools.
15
+
16
+ ### What is MCP?
17
+
18
+ MCP is a protocol for AI agents to discover and call tools. By integrating MCP with Kratos services, you can:
19
+
20
+ - Expose Kratos APIs as MCP tools for AI agents
21
+ - Enable tool discovery and schema validation
22
+ - Handle structured input/output for AI workflows
23
+
24
+ ### Library Options
25
+
26
+ Two main Go MCP libraries exist:
27
+
28
+ | Library | Maintainer | Use Case |
29
+ |---------|------------|----------|
30
+ | `github.com/mark3labs/mcp-go` | Mark3 Labs | Community implementation |
31
+ | `github.com/modelcontextprotocol/go-sdk` | Anthropic | Official SDK |
32
+
33
+ **Note:** No official Kratos MCP transport exists yet. Integration requires custom setup.
34
+
35
+ ### Integration Approach: MCP Handler in Kratos HTTP Server
36
+
37
+ Add MCP endpoint to existing Kratos HTTP server:
38
+
39
+ ```go
40
+ import (
41
+ "context"
42
+ "fmt"
43
+ "github.com/go-kratos/kratos/v2"
44
+ "github.com/go-kratos/kratos/v2/log"
45
+ "github.com/go-kratos/kratos/v2/transport/http"
46
+ mcp "github.com/mark3labs/mcp-go/mcp"
47
+ "github.com/mark3labs/mcp-go/server"
48
+ )
49
+
50
+ func main() {
51
+ // Create MCP server
52
+ mcpSrv := server.NewMCPServer("kratos-mcp", "v1.0.0")
53
+
54
+ // Define tool
55
+ tool := mcp.NewTool("hello_world",
56
+ mcp.WithDescription("Say hello to someone"),
57
+ mcp.WithString("name",
58
+ mcp.Required(),
59
+ mcp.Description("Name of the person to greet"),
60
+ ),
61
+ )
62
+
63
+ // Add tool handler
64
+ mcpSrv.AddTool(tool, helloHandler)
65
+
66
+ // Create Kratos HTTP server with MCP endpoint
67
+ httpSrv := http.NewServer(http.Address(":8000"))
68
+
69
+ // Register MCP handler on Kratos HTTP server
70
+ // MCP typically uses SSE (Server-Sent Events) transport
71
+ route := httpSrv.Route("/")
72
+ route.GET("/mcp", func(ctx http.Context) error {
73
+ // Handle MCP SSE requests
74
+ return handleMCPRequest(ctx, mcpSrv)
75
+ })
76
+
77
+ // Create Kratos app
78
+ app := kratos.New(
79
+ kratos.Name("kratos-mcp"),
80
+ kratos.Server(httpSrv),
81
+ )
82
+
83
+ if err := app.Run(); err != nil {
84
+ panic(err)
85
+ }
86
+ }
87
+ ```
88
+
89
+ ### Tool Handler
90
+
91
+ ```go
92
+ func helloHandler(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
93
+ // Extract arguments
94
+ name, ok := request.Params.Arguments["name"].(string)
95
+ if !ok {
96
+ return nil, errors.New("name must be a string")
97
+ }
98
+
99
+ // Return result
100
+ return mcp.NewToolResultText(fmt.Sprintf("Hello, %s!", name)), nil
101
+ }
102
+ ```
103
+
104
+ ### Multiple Tools
105
+
106
+ ```go
107
+ func registerMCPTools(mcpSrv *server.MCPServer, userUC *biz.UserUseCase, orderUC *biz.OrderUseCase) {
108
+ // Tool 1: User lookup
109
+ userTool := mcp.NewTool("get_user",
110
+ mcp.WithDescription("Get user by ID"),
111
+ mcp.WithString("user_id",
112
+ mcp.Required(),
113
+ mcp.Description("User identifier"),
114
+ ),
115
+ )
116
+ mcpSrv.AddTool(userTool, func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
117
+ userID, ok := req.Params.Arguments["user_id"].(string)
118
+ if !ok {
119
+ return mcp.NewToolResultError("user_id must be a string"), nil
120
+ }
121
+ // Call biz layer
122
+ user, err := userUC.GetUser(ctx, userID)
123
+ if err != nil {
124
+ return mcp.NewToolResultError(err.Error()), nil
125
+ }
126
+ return mcp.NewToolResultText(fmt.Sprintf("User: %s, Email: %s", user.Name, user.Email)), nil
127
+ })
128
+
129
+ // Tool 2: Order creation
130
+ orderTool := mcp.NewTool("create_order",
131
+ mcp.WithDescription("Create a new order"),
132
+ mcp.WithString("user_id", mcp.Required()),
133
+ mcp.WithString("product_id", mcp.Required()),
134
+ mcp.WithNumber("quantity", mcp.Required()),
135
+ )
136
+ mcpSrv.AddTool(orderTool, func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
137
+ userID, ok := req.Params.Arguments["user_id"].(string)
138
+ if !ok {
139
+ return mcp.NewToolResultError("user_id must be a string"), nil
140
+ }
141
+ productID, ok := req.Params.Arguments["product_id"].(string)
142
+ if !ok {
143
+ return mcp.NewToolResultError("product_id must be a string"), nil
144
+ }
145
+ quantity, ok := req.Params.Arguments["quantity"].(float64)
146
+ if !ok {
147
+ return mcp.NewToolResultError("quantity must be a number"), nil
148
+ }
149
+
150
+ // Call biz layer
151
+ order, err := orderUC.CreateOrder(ctx, userID, productID, int(quantity))
152
+ if err != nil {
153
+ return mcp.NewToolResultError(err.Error()), nil
154
+ }
155
+
156
+ return mcp.NewToolResultText(fmt.Sprintf("Order created: %s", order.ID)), nil
157
+ })
158
+ }
159
+ ```
160
+
161
+ ### MCP with Kratos Middleware
162
+
163
+ Wrap MCP endpoint with Kratos middleware for logging, auth, etc.:
164
+
165
+ ```go
166
+ func NewHTTPServer(c *conf.Server, mcpSrv *server.MCPServer, logger log.Logger) *http.Server {
167
+ srv := http.NewServer(
168
+ http.Address(c.Http.Addr),
169
+ http.Middleware(
170
+ recovery.Recovery(),
171
+ tracing.Server(),
172
+ logging.Server(logger),
173
+ ),
174
+ )
175
+
176
+ // MCP endpoint with middleware chain
177
+ route := srv.Route("/")
178
+ route.GET("/mcp", func(ctx http.Context) error {
179
+ http.SetOperation(ctx, "/mcp/handle")
180
+ h := ctx.Middleware(func(ctx context.Context, req interface{}) (interface{}, error) {
181
+ return handleMCPRequest(ctx, mcpSrv)
182
+ })
183
+ resp, err := h(ctx, nil)
184
+ if err != nil {
185
+ return err
186
+ }
187
+ return ctx.JSON(200, resp)
188
+ })
189
+
190
+ return srv
191
+ }
192
+ ```
193
+
194
+ ### Health Check
195
+
196
+ Add health check alongside MCP endpoint:
197
+
198
+ ```go
199
+ route := srv.Route("/")
200
+ route.GET("/health/ready", func(ctx http.Context) error {
201
+ return ctx.JSON(200, map[string]string{"status": "ok"})
202
+ })
203
+ route.GET("/mcp", handleMCP)
204
+ ```
205
+
206
+ ---
207
+
208
+ ## MCP Tool Schema Types
209
+
210
+ ### String Parameter
211
+
212
+ ```go
213
+ mcp.WithString("name",
214
+ mcp.Required(),
215
+ mcp.Description("Description of parameter"),
216
+ mcp.Pattern("^[a-z]+$"), // Optional: regex pattern
217
+ )
218
+ ```
219
+
220
+ ### Number Parameter
221
+
222
+ ```go
223
+ mcp.WithNumber("quantity",
224
+ mcp.Required(),
225
+ mcp.Description("Number of items"),
226
+ mcp.MinNumber(1), // Optional: minimum
227
+ mcp.MaxNumber(100), // Optional: maximum
228
+ )
229
+ ```
230
+
231
+ ### Boolean Parameter
232
+
233
+ ```go
234
+ mcp.WithBoolean("enabled",
235
+ mcp.Description("Enable feature"),
236
+ )
237
+ ```
238
+
239
+ ### Object Parameter
240
+
241
+ ```go
242
+ mcp.WithObject("config",
243
+ mcp.Description("Configuration object"),
244
+ mcp.Properties(map[string]mcp.Property{
245
+ "timeout": mcp.NewNumberProperty(30),
246
+ "retries": mcp.NewNumberProperty(3),
247
+ }),
248
+ )
249
+ ```
250
+
251
+ ---
252
+
253
+ ## Result Types
254
+
255
+ ### Text Result
256
+
257
+ ```go
258
+ mcp.NewToolResultText("Operation completed successfully")
259
+ ```
260
+
261
+ ### Error Result
262
+
263
+ ```go
264
+ mcp.NewToolResultError("Invalid input: user_id required")
265
+ ```
266
+
267
+ ### JSON Result
268
+
269
+ ```go
270
+ mcp.NewToolResultJSON(map[string]interface{}{
271
+ "user_id": "123",
272
+ "name": "John",
273
+ "email": "john@example.com",
274
+ })
275
+ ```
276
+
277
+ ---
278
+
279
+ ## MCP Client Integration
280
+
281
+ AI agents discover and call tools via MCP protocol:
282
+
283
+ 1. **Discovery**: Agent calls `list_tools` to get available tools
284
+ 2. **Schema**: Agent receives tool definitions with parameters
285
+ 3. **Invocation**: Agent calls `call_tool` with arguments
286
+ 4. **Response**: Server returns structured result
287
+
288
+ ### Integration Example
289
+
290
+ Claude Code MCP client configuration:
291
+
292
+ ```json
293
+ {
294
+ "mcpServers": {
295
+ "kratos-mcp": {
296
+ "url": "http://localhost:8000/mcp"
297
+ }
298
+ }
299
+ }
300
+ ```
301
+
302
+ ---
303
+
304
+ ## Future Extensions
305
+
306
+ This reference file can be extended with:
307
+
308
+ - Plugin system integration
309
+ - Custom transport protocols
310
+ - Service mesh patterns
311
+ - GraphQL gateway
312
+ - Event-driven architecture
313
+
314
+ Check official Kratos documentation for updates: https://go-kratos.dev