Commit 35b3345
committed
Add Callbacks Feature
MCPServer is upgraded with the capacity to notify before and/or
after requests run. Developers can choose Before / After events,
and can submit callbacks that are notified during EVERY request
lifecycle, or else specific request methods. The specific request
method callbacks receive typed message and/or result objects.
A special OnError callback is also provided.
To accomplish this with a minimal margin for error, a bit of a
refactoring was done:
- The `MCPServer.HandleMessage` method was moved from server.go
into its own file.
- The individual request handlers' prototypes were changed.
rather than returning `mcp.JSONRPCMessage`, they return a tuple
of their specific result type and error.
- A code generation schema was introduced, to facilitate adding
functionality to each clause in the switch statement within
`HandleMessage()`.
- Now that HandleMessage() now receives a typed result object
and possible error, it is able to pass those results to any
configured callbacks.
I considered several options including a strategy pattern to make
HandleMessage() more DRY, but opted to stick with the minimal
reorganization of the code, and to keep the algorithm as flat
and obvious as possible. I think the codegen mitigates the
repetitiveness of that section of code.1 parent 771de1d commit 35b3345
File tree
13 files changed
+1307
-241
lines changed- examples/everything
- mcp
- server
- internal/gen
13 files changed
+1307
-241
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
509 | 509 | | |
510 | 510 | | |
511 | 511 | | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
512 | 524 | | |
513 | 525 | | |
514 | 526 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 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 | + | |
32 | 57 | | |
33 | 58 | | |
34 | 59 | | |
35 | 60 | | |
36 | 61 | | |
37 | 62 | | |
| 63 | + | |
38 | 64 | | |
39 | 65 | | |
40 | 66 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
13 | | - | |
14 | 14 | | |
15 | 15 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
11 | 25 | | |
12 | 26 | | |
13 | 27 | | |
| |||
0 commit comments