CMSIS-MCP  Version 0.0.0
Embedded MCP server with serial transport
 
Loading...
Searching...
No Matches
Request dispatch and transport

Serve complete JSON-RPC messages with synchronous response delivery. More...

Typedefs

typedef void(* mcp_send_fn) (const char *json, int fd)
 Synchronous transport callback used by dispatch_with_sender().
 

Functions

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.
 

Description

Serve complete JSON-RPC messages with synchronous response delivery.

Typedef Documentation

◆ mcp_send_fn

typedef void(* mcp_send_fn) (const char *json, int fd)

Synchronous transport callback used by dispatch_with_sender().

Parameters
[in]jsonBorrowed, null-terminated JSON without a newline; NULL denotes a notification with no JSON-RPC response.
[in]fdOpaque 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); }
}

Function Documentation

◆ dispatch()

void dispatch ( const char * line,
int fd )
extern

Dispatch one JSON-RPC message through the default transport hook.

Parameters
[in]lineComplete, null-terminated JSON text; must not be NULL.
[in]fdOpaque 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]lineComplete, null-terminated JSON text; must not be NULL.
[in]fdOpaque transport/channel identifier passed unchanged to send.
[in]sendSynchronous 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
dispatch_with_sender("{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"ping\"}",
7, send_reply); // send_reply receives channel 7.
void dispatch_with_sender(const char *line, int fd, mcp_send_fn send)
Dispatch one JSON-RPC message through an application-provided sender.