Repository navigation
Expand file tree
/
Copy pathwebmcp.go
More file actions
103 lines (94 loc) · 3.4 KB
/
Copy pathwebmcp.go
File metadata and controls
103 lines (94 loc) · 3.4 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
package webmcp
import (
"webtyp.com/agenttools"
"webtyp.com/appkit"
"webtyp.com/context"
"webtyp.com/fmt"
"webtyp.com/llm"
"webtyp.com/model"
"webtyp.com/router"
"webtyp.com/view"
)
const (
errNoCaller = "webmcp: New needs the caller of the session (screens.Browser.Caller): the tools would have no way to reach the server"
errNotOnScreen = "this tool is not on the screen the person has open; open its screen first: "
)
// Tools offers an AI agent, while a screen is open, the tools of the entities that screen shows and the app opened
// (appkit.App.OpenToAgents): it registers them in the browser's WebMCP (document.modelContext) and removes them when
// the person leaves. A browser agent (Chrome's) finds them there; an agent inside the page reads them with Available
// and runs them with Run. Every call goes through the session's caller, so it has the person's own permissions.
//
// agentTools, err := webmcp.New(screens.Browser.Caller, config.App)
// layoutapp.Tools{ScreenWatchers: []view.ScreenWatcher{agentTools}}
type Tools struct {
caller router.Caller
opened []screenTool
shown []agenttools.Tool
registration registration // what is registered in document.modelContext now (browser only)
}
// screenTool is one tool the app opened and the resource of the screens that show its entity.
type screenTool struct {
resource model.Resource
tool agenttools.Tool
}
var _ view.ScreenWatcher = (*Tools)(nil)
// New builds the tools of every entity the app opened to agents. It fails at startup, never at a call, when the app
// opens something it cannot (appkit.App.AgentOperations, agenttools.For).
func New(caller router.Caller, app appkit.App) (*Tools, error) {
if caller == nil {
return nil, fmt.Err(errNoCaller)
}
ops, err := app.AgentOperations()
if err != nil {
return nil, err
}
t := &Tools{caller: caller}
for _, o := range ops {
tool, err := agenttools.For(o.Op, o.Entity, app.Modules)
if err != nil {
return nil, err
}
t.opened = append(t.opened, screenTool{resource: model.ResourceOf(o.Entity), tool: tool})
}
return t, nil
}
// ScreenShown offers the tools of the entity the screen shows (its Permission) and withdraws those of the screen before.
// A screen that shows no opened entity offers nothing.
func (t *Tools) ScreenShown(s view.Screen) {
var shown []agenttools.Tool
for _, o := range t.opened {
if s.Permission != "" && o.resource == s.Permission {
shown = append(shown, o.tool)
}
}
t.shown = shown
t.register(shown)
}
// Available is the tools of the screen the person has open, in the order the app opened them: what an agent reads
// (agent.ToolSource), never how they run — Run runs them.
func (t *Tools) Available() []llm.ToolDef {
out := make([]llm.ToolDef, len(t.shown))
for i, tool := range t.shown {
out[i] = tool.ToolDef
}
return out
}
// Run runs one tool of the open screen with the agent's arguments (a JSON object) and waits for its reply. It blocks:
// call it from the agent's own goroutine, never from a browser event handler.
func (t *Tools) Run(_ *context.Context, name, input string) (string, error) {
for _, tool := range t.shown {
if tool.Name != name {
continue
}
done := make(chan struct{}, 1)
var reply string
var runErr error
tool.Run(input, t.caller, func(r string, err error) {
reply, runErr = r, err
done <- struct{}{}
})
<-done
return reply, runErr
}
return "", fmt.Err(errNotOnScreen, name)
}