Initial project import
This commit is contained in:
commit
23f2d29e32
11957 files changed
+1754577
No files matched your search
@@ -0,0 +1,18 @@
|
||||
# mcp integration example
|
||||
|
||||
This example uses MCP JSON-RPC values in memory and runs the dispatch step in an
|
||||
`x.async.Task`. It does not start stdio, HTTP, a process transport, or any
|
||||
external service.
|
||||
|
||||
The goal is to show the integration boundary:
|
||||
|
||||
- build an MCP request value;
|
||||
- decode and validate it inside a task;
|
||||
- return an MCP response value;
|
||||
- consume the result with `Task.wait()`.
|
||||
|
||||
Run from the repository root:
|
||||
|
||||
```sh
|
||||
./v run vlib/x/async/examples/mcp/tool_dispatch.v
|
||||
```
|
||||
@@ -0,0 +1,37 @@
|
||||
import context
|
||||
import mcp
|
||||
import x.async as xasync
|
||||
|
||||
struct EchoArgs {
|
||||
pub:
|
||||
text string
|
||||
}
|
||||
|
||||
fn dispatch_tool(raw string) !mcp.Response {
|
||||
req := mcp.decode_request(raw)!
|
||||
if req.method != 'tools/call' {
|
||||
return error('unsupported MCP method')
|
||||
}
|
||||
args := req.decode_params[EchoArgs]()!
|
||||
result := mcp.tool_text_result('echo: ${args.text}')
|
||||
return mcp.new_response(1, result, mcp.ResponseError{})
|
||||
}
|
||||
|
||||
fn main() {
|
||||
request := mcp.new_request(1, 'tools/call', EchoArgs{
|
||||
text: 'hello'
|
||||
})
|
||||
mut task := xasync.run[mcp.Response](fn [request] (mut ctx context.Context) !mcp.Response {
|
||||
done := ctx.done()
|
||||
select {
|
||||
_ := <-done {
|
||||
return ctx.err()
|
||||
}
|
||||
else {}
|
||||
}
|
||||
return dispatch_tool(request.encode())!
|
||||
})!
|
||||
|
||||
response := task.wait()!
|
||||
println(response.encode())
|
||||
}
|
||||
Reference in new issue
Block a user