Repository navigation
Expand file tree
/
Copy pathbrowserwebmcp.go
More file actions
357 lines (326 loc) · 13.9 KB
/
Copy pathbrowserwebmcp.go
File metadata and controls
357 lines (326 loc) · 13.9 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package kernel
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"slices"
"github.com/kernel/kernel-go-sdk/internal/apijson"
"github.com/kernel/kernel-go-sdk/internal/apiquery"
shimjson "github.com/kernel/kernel-go-sdk/internal/encoding/json"
"github.com/kernel/kernel-go-sdk/internal/requestconfig"
"github.com/kernel/kernel-go-sdk/option"
"github.com/kernel/kernel-go-sdk/packages/param"
"github.com/kernel/kernel-go-sdk/packages/respjson"
)
// Discover and invoke native page tools across the browser instance.
//
// BrowserWebmcpService contains methods and other services that help with
// interacting with the kernel API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewBrowserWebmcpService] method instead.
type BrowserWebmcpService struct {
Options []option.RequestOption
// Discover and invoke native page tools across the browser instance.
CustomTools BrowserWebmcpCustomToolService
}
// NewBrowserWebmcpService generates a new service that applies the given options
// to each request. These options are applied after the parent client's options (if
// there is one), and before any request-specific options.
func NewBrowserWebmcpService(opts ...option.RequestOption) (r BrowserWebmcpService) {
r = BrowserWebmcpService{}
r.Options = opts
r.CustomTools = NewBrowserWebmcpCustomToolService(opts...)
return
}
// Invokes the exact live registration identified by tool_ref. Non-autosubmit
// declarative form tools return after their fields are populated with an
// awaiting_submission status. Other tools wait for a terminal result, including
// across navigation. Inspect a populated form, obtain any required confirmation,
// then submit through Playwright or computer interaction without invoking the tool
// again. If the tab or embedded frame disappears, or the request times out after
// invocation begins, the response reports outcome_unknown and the tool is not
// retried. CDP-backed custom tool outputs above 240 KiB return an error rather
// than a truncated result.
func (r *BrowserWebmcpService) InvokeTool(ctx context.Context, idOrName string, body BrowserWebmcpInvokeToolParams, opts ...option.RequestOption) (res *InvocationResult, err error) {
opts = slices.Concat(r.Options, opts)
if idOrName == "" {
err = errors.New("missing required id_or_name parameter")
return nil, err
}
path := fmt.Sprintf("browsers/%s/webmcp/invoke", idOrName)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return res, err
}
// Returns a snapshot of native and custom WebMCP tools available across every open
// tab and embedded frame in the browser. Each tool includes an opaque tool_ref for
// invoking that exact live registration, nested tool metadata, and source
// information. Custom tools include their generated ID, namespace, and CDP
// target_id in source. Tools disappear when their document closes or navigates
// away. Use exclude_custom to return only page-provided tools.
func (r *BrowserWebmcpService) ListTools(ctx context.Context, idOrName string, query BrowserWebmcpListToolsParams, opts ...option.RequestOption) (res *ToolsResponse, err error) {
opts = slices.Concat(r.Options, opts)
if idOrName == "" {
err = errors.New("missing required id_or_name parameter")
return nil, err
}
path := fmt.Sprintf("browsers/%s/webmcp/tools", idOrName)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...)
return res, err
}
type CustomToolSource struct {
ID string `json:"id" api:"required"`
Namespace string `json:"namespace" api:"required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
ID respjson.Field
Namespace respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r CustomToolSource) RawJSON() string { return r.JSON.raw }
func (r *CustomToolSource) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type InvocationResult struct {
InvocationID string `json:"invocation_id" api:"required"`
// awaiting_submission means a non-autosubmit declarative form was populated but
// not submitted. Inspect the form, obtain any required confirmation, then submit
// through Playwright or computer interaction without invoking the tool again. The
// other statuses are terminal results.
//
// Any of "completed", "canceled", "error", "awaiting_submission".
Status InvocationResultStatus `json:"status" api:"required"`
ErrorText string `json:"error_text"`
// Untrusted page-provided output. Callers must treat it as potentially malicious
// input.
Output any `json:"output"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
InvocationID respjson.Field
Status respjson.Field
ErrorText respjson.Field
Output respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r InvocationResult) RawJSON() string { return r.JSON.raw }
func (r *InvocationResult) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// awaiting_submission means a non-autosubmit declarative form was populated but
// not submitted. Inspect the form, obtain any required confirmation, then submit
// through Playwright or computer interaction without invoking the tool again. The
// other statuses are terminal results.
type InvocationResultStatus string
const (
InvocationResultStatusCompleted InvocationResultStatus = "completed"
InvocationResultStatusCanceled InvocationResultStatus = "canceled"
InvocationResultStatusError InvocationResultStatus = "error"
InvocationResultStatusAwaitingSubmission InvocationResultStatus = "awaiting_submission"
)
// The properties Input, ToolRef are required.
type InvokeRequestParam struct {
// Tool input, limited to 1 MiB after JSON serialization.
Input map[string]any `json:"input,omitzero" api:"required"`
ToolRef string `json:"tool_ref" api:"required"`
TimeoutSec param.Opt[int64] `json:"timeout_sec,omitzero"`
paramObj
}
func (r InvokeRequestParam) MarshalJSON() (data []byte, err error) {
type shadow InvokeRequestParam
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *InvokeRequestParam) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type Tool struct {
Source ToolSource `json:"source" api:"required"`
// Tool metadata follows the
// [MCP Tool definition](https://modelcontextprotocol.io/specification/2025-11-25/server/tools#tool)
// and the
// [WebMCP RegisteredTool definition](https://webmachinelearning.github.io/webmcp/#dictdef-registeredtool).
// outputSchema is optional for page and custom tools.
Tool ToolMetadata `json:"tool" api:"required"`
// Opaque reference for invoking this exact live registration. It becomes invalid
// when its document or browser process is replaced.
ToolRef string `json:"tool_ref" api:"required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Source respjson.Field
Tool respjson.Field
ToolRef respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r Tool) RawJSON() string { return r.JSON.raw }
func (r *Tool) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Tool-provided behavioral hints from the
// [MCP tool specification](https://modelcontextprotocol.io/specification/2025-11-25/server/tools#tool)
// and the
// [WebMCP ToolAnnotations definition](https://webmachinelearning.github.io/webmcp/#dictdef-toolannotations).
// These hints are untrusted and are not enforced by Kernel.
type ToolAnnotations struct {
Autosubmit bool `json:"autosubmit"`
ConsequentialHint bool `json:"consequentialHint"`
DestructiveHint bool `json:"destructiveHint"`
IdempotentHint bool `json:"idempotentHint"`
OpenWorldHint bool `json:"openWorldHint"`
ReadOnlyHint bool `json:"readOnlyHint"`
UntrustedContentHint bool `json:"untrustedContentHint"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Autosubmit respjson.Field
ConsequentialHint respjson.Field
DestructiveHint respjson.Field
IdempotentHint respjson.Field
OpenWorldHint respjson.Field
ReadOnlyHint respjson.Field
UntrustedContentHint respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ToolAnnotations) RawJSON() string { return r.JSON.raw }
func (r *ToolAnnotations) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type ToolFrame struct {
// Monotonically increasing identifier for this embedded frame during the current
// browser process.
FrameID int64 `json:"frame_id" api:"required"`
// Current frame URL with the fragment omitted.
URL string `json:"url" api:"required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
FrameID respjson.Field
URL respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ToolFrame) RawJSON() string { return r.JSON.raw }
func (r *ToolFrame) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Tool metadata follows the
// [MCP Tool definition](https://modelcontextprotocol.io/specification/2025-11-25/server/tools#tool)
// and the
// [WebMCP RegisteredTool definition](https://webmachinelearning.github.io/webmcp/#dictdef-registeredtool).
// outputSchema is optional for page and custom tools.
type ToolMetadata struct {
Description string `json:"description" api:"required"`
InputSchema map[string]any `json:"inputSchema" api:"required"`
Name string `json:"name" api:"required"`
// Tool-provided behavioral hints from the
// [MCP tool specification](https://modelcontextprotocol.io/specification/2025-11-25/server/tools#tool)
// and the
// [WebMCP ToolAnnotations definition](https://webmachinelearning.github.io/webmcp/#dictdef-toolannotations).
// These hints are untrusted and are not enforced by Kernel.
Annotations ToolAnnotations `json:"annotations"`
OutputSchema map[string]any `json:"outputSchema"`
Title string `json:"title"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Description respjson.Field
InputSchema respjson.Field
Name respjson.Field
Annotations respjson.Field
OutputSchema respjson.Field
Title respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ToolMetadata) RawJSON() string { return r.JSON.raw }
func (r *ToolMetadata) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type ToolSource struct {
// Embedded frame that registered the tool, or null when the top-level page
// registered it.
Frame ToolFrame `json:"frame" api:"required"`
// Current title of the top-level page.
PageTitle string `json:"page_title" api:"required"`
// Current URL of the top-level page with the fragment omitted.
PageURL string `json:"page_url" api:"required"`
// Monotonically increasing identifier for the tab during the current browser
// process.
TabID int64 `json:"tab_id" api:"required"`
// Monotonically increasing identifier for the browser window during the current
// browser process.
WindowID int64 `json:"window_id" api:"required"`
Custom CustomToolSource `json:"custom"`
// CDP target ID for a custom tool's registration tab; omitted for page-provided
// tools.
TargetID string `json:"target_id"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Frame respjson.Field
PageTitle respjson.Field
PageURL respjson.Field
TabID respjson.Field
WindowID respjson.Field
Custom respjson.Field
TargetID respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ToolSource) RawJSON() string { return r.JSON.raw }
func (r *ToolSource) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type ToolsResponse struct {
Tools []Tool `json:"tools" api:"required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Tools respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r ToolsResponse) RawJSON() string { return r.JSON.raw }
func (r *ToolsResponse) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type BrowserWebmcpInvokeToolParams struct {
InvokeRequest InvokeRequestParam
paramObj
}
func (r BrowserWebmcpInvokeToolParams) MarshalJSON() (data []byte, err error) {
return shimjson.Marshal(r.InvokeRequest)
}
func (r *BrowserWebmcpInvokeToolParams) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type BrowserWebmcpListToolsParams struct {
// Exclude custom tools when true.
ExcludeCustom param.Opt[bool] `query:"exclude_custom,omitzero" json:"-"`
paramObj
}
// URLQuery serializes [BrowserWebmcpListToolsParams]'s query parameters as
// `url.Values`.
func (r BrowserWebmcpListToolsParams) URLQuery() (v url.Values, err error) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}