CMSIS-MCP  Version 0.0.0
Embedded MCP server with serial transport
 
Loading...
Searching...
No Matches
Startup and request memory

Prepare persistent metadata and manage bounded request storage. More...

Functions

int mcp_prepare (void)
 Finish startup by caching discovery schemas and response templates.
 
void * mcp_arena_alloc (size_t size)
 Allocate aligned storage from the current request arena.
 
char * mcp_arena_strdup (const char *text)
 Copy a null-terminated string into the current request arena.
 
void free_tools (void)
 Release tool/resource registries and cached responses; reopen registration.
 

Description

Prepare persistent metadata and manage bounded request storage.

Function Documentation

◆ free_tools()

void free_tools ( void )
extern

Release tool/resource registries and cached responses; reopen registration.

Invalidates all handles. Borrowed registration strings are not freed. Repeated calls are safe; a call during dispatch is ignored. Arena high-water and cJSON heap-allocation counters are not reset.

Example
free_tools(); // After stopping the request loop, not inside a callback.
void free_tools(void)
Release tool/resource registries and cached responses; reopen registration.

◆ mcp_arena_alloc()

void * mcp_arena_alloc ( size_t size)
extern

Allocate aligned storage from the current request arena.

Parameters
[in]sizeNumber of bytes; must be greater than zero.
Returns
Borrowed storage, or NULL outside dispatch, for zero size or exhaustion.

Aligns for the core's scalar types. Exhaustion marks the request as failed; dispatch sends a bounded internal error and subsequent requests can recover. No individual free: reset occurs after the synchronous sender returns.

Example
char *text = (char *)mcp_arena_alloc(64); // Inside a callback only.
void * mcp_arena_alloc(size_t size)
Allocate aligned storage from the current request arena.
See also
add_resource for formatted text and allocation/error checks.

◆ mcp_arena_strdup()

char * mcp_arena_strdup ( const char * text)
extern

Copy a null-terminated string into the current request arena.

Parameters
[in]textSource string; NULL is permitted.
Returns
Borrowed copy including its null terminator, or NULL for NULL input, exhaustion or a call outside dispatch.

Follows the lifetime/exhaustion rules of mcp_arena_alloc().

Example
*returnMessage = mcp_arena_strdup("Measurement ready"); // Inside a callback.
if (!*returnMessage) { *returnMessage = "Arena exhausted"; return MCP_INTERNAL_ERROR; }
return 0;
char * mcp_arena_strdup(const char *text)
Copy a null-terminated string into the current request arena.
#define MCP_INTERNAL_ERROR
JSON-RPC internal error: allocation or callback failure (-32603).
Definition mcp.h:87

◆ mcp_prepare()

int mcp_prepare ( void )
extern

Finish startup by caching discovery schemas and response templates.

Returns
0 on success, including repeated calls after successful preparation; -1 for allocation/registration failure or an active unprepared request.

Successful preparation closes registration until free_tools(). Dispatch also prepares on first use. Explicit preparation lets startup detect errors before accepting requests. Installs process-wide cJSON hooks and uses the heap, not the request arena.

Example
if (mcp_prepare() != 0) { free_tools(); return -1; }
int mcp_prepare(void)
Finish startup by caching discovery schemas and response templates.