Serve complete JSON-RPC messages with synchronous response delivery.
More...
|
| void | dispatch (const char *line, int fd) |
| | Dispatch one JSON-RPC message through the default transport hook.
|
| |
| void | dispatch_with_sender (const char *line, int fd, mcp_send_fn send) |
| | Dispatch one JSON-RPC message through an application-provided sender.
|
| |
Serve complete JSON-RPC messages with synchronous response delivery.
◆ mcp_send_fn
| typedef void(* mcp_send_fn) (const char *json, int fd) |
Synchronous transport callback used by dispatch_with_sender().
- Parameters
-
| [in] | json | Borrowed, null-terminated JSON without a newline; NULL denotes a notification with no JSON-RPC response. |
| [in] | fd | Opaque integer supplied by dispatch_with_sender(), passed unchanged. |
- Warning
- Complete transmission before returning. Do not retain the pointer or dispatch another request from this callback. An asynchronous/DMA transport must wait for completion or copy into transport-owned storage first.
Serial/stdout senders emit nothing for NULL.
- Example
#include <stdio.h>
static void send_reply(const char *json, int channel)
{
(void)channel;
if (json) { puts(json); fflush(stdout); }
}
◆ dispatch()
| void dispatch |
( |
const char * | line, |
|
|
int | fd ) |
|
extern |
Dispatch one JSON-RPC message through the default transport hook.
- Parameters
-
| [in] | line | Complete, null-terminated JSON text; must not be NULL. |
| [in] | fd | Opaque channel passed to mcp_serial_send(); ignored by its default. |
The mcp_serial_send() default writes JSON followed by LF and flushes stdout. On CMSIS boards it is weak and can be overridden for another interface; see serial_transport.h. The application supplies its input loop or uses the optional process_serial() loop. Notifications emit nothing and do not invoke tool/resource callbacks. The request arena has been reset when this function returns.
- Example
dispatch(
"{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/list\"}", 0);
void dispatch(const char *line, int fd)
Dispatch one JSON-RPC message through the default transport hook.
◆ dispatch_with_sender()
| void dispatch_with_sender |
( |
const char * | line, |
|
|
int | fd, |
|
|
mcp_send_fn | send ) |
|
extern |
Dispatch one JSON-RPC message through an application-provided sender.
- Parameters
-
| [in] | line | Complete, null-terminated JSON text; must not be NULL. |
| [in] | fd | Opaque transport/channel identifier passed unchanged to send. |
| [in] | send | Synchronous response callback; NULL selects mcp_serial_send(). |
Handles initialize, ping, tools/list and tools/call, and optionally resources/list and resources/read. Rejects malformed envelopes, trailing input and unsupported methods. Notifications do not invoke tool/resource callbacks. On arena exhaustion, independent bounded storage supplies an error; the next request can recover. Reentrant requests receive an internal error.
- Example
7, send_reply);
void dispatch_with_sender(const char *line, int fd, mcp_send_fn send)
Dispatch one JSON-RPC message through an application-provided sender.