direct_dispatcher
In-memory Dispatcher that wires two peers together with no transport.
DirectDispatcher is the simplest possible Dispatcher implementation: a
request on one side directly invokes the other side's on_request. There is no
serialization, no JSON-RPC framing, and no streams. It exists to:
- prove the
DispatcherProtocol is implementable without JSON-RPC - provide a fast substrate for testing the layers above the dispatcher
(
ServerRunner,Context,Connection) without wire-level moving parts - embed a server in-process when the JSON-RPC overhead is unnecessary
Unlike JSONRPCDispatcher, exceptions raised in a handler propagate directly
to the caller - there is no exception-to-ErrorData boundary here.
DirectDispatcher
A Dispatcher that calls a peer's handlers directly, in-process.
Two instances are wired together with create_direct_dispatcher_pair; each
holds a reference to the other. send_raw_request on one awaits the peer's
on_request. run parks until close is called.
Lifecycle mirrors JSONRPCDispatcher: send_raw_request requires run()
to have started, and once a side has closed - via close() or run()
ending - send_raw_request raises MCPError (CONNECTION_CLOSED) and
inbound requests fail the peer's call the same way instead of invoking the
handler. Notifications are fire-and-forget in both directions: after close
they are silently dropped.
Source code in src/mcp/shared/direct_dispatcher.py
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 | |
send_raw_request
async
send_raw_request(
method: str,
params: Mapping[str, Any] | None,
opts: CallOptions | None = None,
) -> dict[str, Any]
Send a request by invoking the peer's on_request directly.
Raises:
| Type | Description |
|---|---|
MCPError
|
The peer's handler raised; |
RuntimeError
|
Called before |
Source code in src/mcp/shared/direct_dispatcher.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
notify
async
Send a notification by invoking the peer's on_notify directly.
Fire-and-forget: usable before run() (delivery waits for the peer to
start), and after close it is silently dropped, matching
JSONRPCDispatcher.notify.
Source code in src/mcp/shared/direct_dispatcher.py
136 137 138 139 140 141 142 143 144 145 146 147 148 | |
run
async
run(
on_request: OnRequest,
on_notify: OnNotify,
*,
task_status: TaskStatus[None] = TASK_STATUS_IGNORED
) -> None
Mark this side ready and park until close() is called.
Single-shot, like JSONRPCDispatcher.run: once it returns the
dispatcher stays closed and cannot be restarted.
Source code in src/mcp/shared/direct_dispatcher.py
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 | |
create_direct_dispatcher_pair
create_direct_dispatcher_pair(
*,
can_send_request: bool = True,
headers: Mapping[str, str] | None = None
) -> tuple[DirectDispatcher, DirectDispatcher]
Create two DirectDispatcher instances wired to each other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
can_send_request
|
bool
|
Sets |
True
|
headers
|
Mapping[str, str] | None
|
Sets |
None
|
Returns:
| Type | Description |
|---|---|
DirectDispatcher
|
A |
DirectDispatcher
|
are conventional only. |
Source code in src/mcp/shared/direct_dispatcher.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |