Repository navigation
Expand file tree
/
Copy pathmcp_inputschema_test.go
More file actions
67 lines (61 loc) · 2.33 KB
/
Copy pathmcp_inputschema_test.go
File metadata and controls
67 lines (61 loc) · 2.33 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
package sqlmcp
import (
"strings"
"testing"
"webtyp.com/context"
"webtyp.com/json"
"webtyp.com/mcp"
"webtyp.com/model"
)
// encodeMCPMessage serializes an mcp.JSONRPCMessage to its wire JSON using
// webtyp/json (never stdlib), exactly as the transport emits it.
func encodeMCPMessage(resp mcp.JSONRPCMessage) string {
var b []byte
if f, ok := resp.(model.Encodable); ok {
_ = json.Encode(f, &b)
}
return string(b)
}
// TestMCP_ToolsList_InputSchemaIsValidJSONSchema garantiza que las tools db_*
// expongan un inputSchema JSON Schema "object" VÁLIDO en tools/list.
//
// Contrato MCP: inputSchema DEBE ser un objeto JSON Schema. Claude Code valida
// tools/list con Zod y descarta el array COMPLETO si uno es inválido — dejando el
// servidor "Connected" pero sin tools. Este test FALLA mientras el schema se genere
// mal (db_query/db_exec serializando el struct → {"SQL":""}; db_schema/db_export_schema
// con "" → null) y PASA cuando mcp genera el inputSchema desde Tool.Args (Schema()).
// NO debe existir lógica de JSON Schema en sqlmcp.
func TestMCP_ToolsList_InputSchemaIsValidJSONSchema(t *testing.T) {
srv, err := mcp.NewServer(mcp.Config{Name: "test", Version: "1.0.0", Authorize: mcp.AllowAll}, nil)
if err != nil {
t.Fatalf("NewServer: %v", err)
}
tools := NewDaemonProvider().Tools()
for _, tool := range tools {
if e := srv.AddTool(tool); e != nil {
t.Fatalf("AddTool %s: %v", tool.Name, e)
}
}
var ctx context.Context
ctx.Set(mcp.CtxKeyUserID, "u1")
resp := srv.HandleMessage(&ctx, []byte(`{"jsonrpc":"2.0","id":"1","method":"tools/list","params":{}}`))
if resp == nil {
t.Fatal("tools/list devolvió nil")
}
body := encodeMCPMessage(resp)
if strings.Contains(body, `"inputSchema":null`) {
t.Errorf("hay tools con inputSchema:null (inválido)\nbody: %s", body)
}
got := strings.Count(body, `"inputSchema":{"type":"object"`)
if got != len(tools) {
t.Errorf("inputSchema \"object\" válidos = %d, se esperaban %d (uno por tool)\nbody: %s",
got, len(tools), body)
}
// db_query / db_exec deben exponer SQL como string, no el struct serializado.
if strings.Contains(body, `"inputSchema":{"SQL"`) {
t.Errorf("inputSchema es el struct serializado, no JSON Schema\nbody: %s", body)
}
if !strings.Contains(body, `"SQL":{"type":"string"}`) {
t.Errorf("se esperaba properties.SQL como string\nbody: %s", body)
}
}