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 <stdlib.h>
static double application_gain = 1.0;
static int set_gain(int argc, const char **returnMessage, const char **args)
{
(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";
return 0;
}
int main(void)
{
struct tool *tool =
add_tool(
"setGain",
"Set the application gain.");
if (tool == NULL) {
return EXIT_FAILURE;
}
return EXIT_FAILURE;
}
return EXIT_FAILURE;
}
return EXIT_FAILURE;
}
}
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.
#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.