CMSIS-MCP  Version 0.0.0
Embedded MCP server with serial transport
 
Loading...
Searching...
No Matches
Simple C example: setGain

This example shows the complete application sequence for one tool. It belongs in a real project with the MCP component, configuration header and serial implementation described in the integration guide. Enable C_MCP_ENABLE_SERIAL_LOOP to use the supplied input loop.

The setGain tool takes one numeric argument, gain, and updates an application setting. Replace the assignment with the action your application needs.

#include "mcp.h"
#include <stdlib.h>
static double application_gain = 1.0;
static int set_gain(int argc, const char **returnMessage, const char **args)
{
/* The dispatcher has already checked the required numeric argument. */
(void)argc;
double gain = strtod(args[0], NULL);
if (gain < 0.0 || gain > 10.0) {
*returnMessage = "gain must be between 0.0 and 10.0";
}
application_gain = gain;
*returnMessage = "Gain updated"; /* Static text remains valid after return. */
return 0;
}
int main(void)
{
/* Register the tool, its required argument and its callback. */
struct tool *tool = add_tool("setGain", "Set the application gain.");
if (tool == NULL) {
return EXIT_FAILURE;
}
add_argument(tool, "gain", TYPE_FLOAT, "Gain between 0.0 and 10.0.");
if (set_tool_callback(tool, set_gain) != 0) {
return EXIT_FAILURE;
}
/* Register any additional tools here, before preparing MCP. */
/* Prepare MCP once all tools are registered; check before accepting input. */
if (mcp_prepare() != 0) {
return EXIT_FAILURE;
}
if (init_serial() != 0) {
return EXIT_FAILURE;
}
/* Receive complete messages, dispatch them and send their responses. */
/* Other application work can use application_gain here. */
}
/* Stop serial I/O, then release MCP registrations and prepared data. */
return EXIT_SUCCESS;
}
void free_tools(void)
Release tool/resource registries and cached responses; reopen registration.
int mcp_prepare(void)
Finish startup by caching discovery schemas and response templates.
#define MCP_SERIAL_EOF
Read hook result: input closed or unrecoverable input error.
Definition serial_transport.h:53
void end_serial(void)
Reset line-buffer state and call mcp_serial_transport_close().
int init_serial(void)
Reset line-buffer state and call mcp_serial_transport_init().
int process_serial(void)
Consume input until one line is processed, RX is empty or EOF occurs.
struct tool * add_tool(const char *name, const char *description)
Register a tool name and its client-visible description during startup.
void add_argument(struct tool *tool, const char *name, enum type type, const char *description)
Append one required argument to a tool's discovery schema.
int set_tool_callback(struct tool *tool, mcp_tool_fn callback)
Bind the callback invoked when a client calls a registered tool.
#define MCP_INVALID_PARAMS
JSON-RPC invalid parameters: missing, extra or invalid input (-32602).
Definition mcp.h:84
@ TYPE_FLOAT
Finite JSON number; callback receives numeric text.
Definition mcp.h:69
Public API for the portable C MCP server.
Serial line API with a default stdio implementation.

process_serial() assembles newline-delimited JSON messages and calls dispatch() internally. A tools/call request for setGain invokes set_gain(), with args[0] containing the numeric text. The callback returns plain response text; MCP creates the JSON response and sends it through mcp_serial_send().

For example, the following message sets application_gain to 2.0:

{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"setGain","arguments":{"gain":2.0}}}

The loop ends when the serial input closes. An embedded application can use its own shutdown condition instead. Supply the project's serial hooks and complete response transmission before the sender returns; see the serial API. Continue with the automation guide to connect an AI harness or call this tool from a script through the Python bridge.