| 1 | import context |
| 2 | import mcp |
| 3 | import x.async as xasync |
| 4 | |
| 5 | struct EchoArgs { |
| 6 | pub: |
| 7 | text string |
| 8 | } |
| 9 | |
| 10 | fn dispatch_tool(raw string) !mcp.Response { |
| 11 | req := mcp.decode_request(raw)! |
| 12 | if req.method != 'tools/call' { |
| 13 | return error('unsupported MCP method') |
| 14 | } |
| 15 | args := req.decode_params[EchoArgs]()! |
| 16 | result := mcp.tool_text_result('echo: ${args.text}') |
| 17 | return mcp.new_response(1, result, mcp.ResponseError{}) |
| 18 | } |
| 19 | |
| 20 | fn main() { |
| 21 | request := mcp.new_request(1, 'tools/call', EchoArgs{ |
| 22 | text: 'hello' |
| 23 | }) |
| 24 | mut task := xasync.run[mcp.Response](fn [request] (mut ctx context.Context) !mcp.Response { |
| 25 | done := ctx.done() |
| 26 | select { |
| 27 | _ := <-done { |
| 28 | return ctx.err() |
| 29 | } |
| 30 | else {} |
| 31 | } |
| 32 | return dispatch_tool(request.encode())! |
| 33 | })! |
| 34 | |
| 35 | response := task.wait()! |
| 36 | println(response.encode()) |
| 37 | } |
| 38 | |