-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
104 lines (90 loc) · 2.71 KB
/
Copy pathexample_test.go
File metadata and controls
104 lines (90 loc) · 2.71 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
99
100
101
102
103
104
package mcpserver_test
import (
"context"
"encoding/json"
"fmt"
"github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/meigma/codemode"
"github.com/meigma/codemode/authz"
"github.com/meigma/codemode/mcpserver"
)
// Example_officialTransport connects official MCP sessions and calls execute.
//
// This in-memory sample has one process-owned identity, matching a single-user
// stdio host. Multi-user hosts must resolve each request from authenticated,
// host-owned context instead. authz.AllowAll is deliberate in this sample;
// production hosts normally supply policy.
func Example_officialTransport() {
// lookupInput is the records.lookup argument contract.
type lookupInput struct {
// Key is the required record identifier.
Key string `json:"key"`
// Limit is the optional result bound.
Limit *int64 `json:"limit,omitempty"`
}
// lookupOutput is the records.lookup handler result.
type lookupOutput struct {
// Key is the looked-up record identifier.
Key string `json:"key"`
// Count is the resolved optional limit, or zero when omitted.
Count int64 `json:"count"`
}
builder := codemode.New(codemode.Options{Authorizer: authz.AllowAll()})
codemode.Register(builder, codemode.Capability[lookupInput, lookupOutput]{
Name: "records.lookup",
Summary: "Look up one record by key.",
Handler: func(_ context.Context, _ authz.Subject, input lookupInput) (lookupOutput, error) {
count := int64(0)
if input.Limit != nil {
count = *input.Limit
}
return lookupOutput{Key: input.Key, Count: count}, nil
},
})
root, err := builder.Build()
if err != nil {
panic(err)
}
mcpServer, err := mcpserver.New(root, mcpserver.StaticSubject(authz.Subject{ID: "example-user"}))
if err != nil {
panic(err)
}
serverTransport, clientTransport := mcp.NewInMemoryTransports()
serverSession, err := mcpServer.Connect(context.Background(), serverTransport, nil)
if err != nil {
panic(err)
}
client := mcp.NewClient(&mcp.Implementation{Name: "example-client", Version: "1"}, nil)
clientSession, err := client.Connect(context.Background(), clientTransport, nil)
if err != nil {
panic(err)
}
result, err := clientSession.CallTool(context.Background(), &mcp.CallToolParams{
Name: "execute",
Arguments: map[string]any{
"source": `
def main():
return records.lookup(key="alpha", limit=2)
`,
},
})
if err != nil {
panic(err)
}
if result.IsError {
panic(fmt.Sprint(result.Content))
}
encoded, err := json.Marshal(result.StructuredContent)
if err != nil {
panic(err)
}
fmt.Println(string(encoded))
if err := clientSession.Close(); err != nil {
panic(err)
}
if err := serverSession.Close(); err != nil {
panic(err)
}
// Output:
// {"result":{"count":2,"key":"alpha"}}
}