CMSIS-MCP  Version 0.0.0
Embedded MCP server with serial transport
 
Loading...
Searching...
No Matches
Read-only virtual resources

Expose virtual text through resources/list and resources/read. More...

Typedefs

typedef int(* mcp_resource_fn) (const char **returnMessage)
 Callback invoked when resources/read resolves a registered URI.
 

Functions

struct mcp_resource * add_resource (const char *uri, const char *name, const char *description, const char *mime_type, mcp_resource_fn callback)
 Register an exact virtual URI and its read callback during startup.
 

Description

Expose virtual text through resources/list and resources/read.

Typedef Documentation

◆ mcp_resource_fn

typedef int(* mcp_resource_fn) (const char **returnMessage)

Callback invoked when resources/read resolves a registered URI.

Parameters
[out]returnMessageSet *returnMessage to null-terminated static or arena text.
Returns
0 on success. Nonzero codes in [-32768, -32000] are preserved as JSON-RPC errors; other nonzero statuses become MCP_INTERNAL_ERROR. Missing output is also an internal error.

There are no input arguments. Resource failures produce JSON-RPC errors, unlike tool execution failures with isError. Output is borrowed until dispatch completes, so do not return an automatic local buffer.

See also
add_resource for a complete callback example.

Function Documentation

◆ add_resource()

struct mcp_resource * add_resource ( const char * uri,
const char * name,
const char * description,
const char * mime_type,
mcp_resource_fn callback )
extern

Register an exact virtual URI and its read callback during startup.

Parameters
[in]uriUnique, nonempty URI containing a scheme separator (colon).
[in]nameNonempty human-readable resource name.
[in]descriptionContents, units or interpretation; NULL omits this field.
[in]mime_typeMIME type; NULL selects "text/plain".
[in]callbackNon-NULL read callback.
Returns
Borrowed handle, or NULL if VFS is disabled, the definition is invalid, the URI is duplicated, allocation fails or registration is closed.

resources/list exposes uri, name, description and mimeType. resources/read invokes the callback registered for the URI; unknown URIs return MCP_RESOURCE_NOT_FOUND. Matching is exact and case-sensitive, without path normalization. Strings are borrowed until free_tools(). This interface exposes virtual text, not a host filesystem; no writes, subscriptions or URI templates.

Complete example: format a live value in request-arena memory
#include "mcp.h"
#include <stdio.h>
static unsigned sensor_value;
static int read_sensor(const char **message)
{
const size_t capacity = 48;
char *text = (char *)mcp_arena_alloc(capacity);
if (!text) { *message = "Arena exhausted"; return MCP_INTERNAL_ERROR; }
int n = snprintf(text, capacity, "sensor value %u", sensor_value);
if (n < 0 || (size_t)n >= capacity) {
*message = "Sensor response formatting failed";
}
*message = text; // Valid until the synchronous sender returns.
return 0;
}
static int register_sensor(void)
{
#if C_MCP_ENABLE_VFS
if (!add_resource("sensor://board/value", "Sensor value",
"Latest sensor measurement", "text/plain", read_sensor)) return -1;
#endif
return mcp_prepare(); // Register any tools before this call too.
}
void * mcp_arena_alloc(size_t size)
Allocate aligned storage from the current request arena.
int mcp_prepare(void)
Finish startup by caching discovery schemas and response templates.
struct mcp_resource * add_resource(const char *uri, const char *name, const char *description, const char *mime_type, mcp_resource_fn callback)
Register an exact virtual URI and its read callback during startup.
#define MCP_INTERNAL_ERROR
JSON-RPC internal error: allocation or callback failure (-32603).
Definition mcp.h:87
Public API for the portable C MCP server.
Read this resource with:
{"jsonrpc":"2.0","id":2,"method":"resources/read","params":{"uri":"sensor://board/value"}}