cMCP 0.4.1
Model Context Protocol library in pure C11
Loading...
Searching...
No Matches
cmcp_schema.h
Go to the documentation of this file.
1/**
2 * @file cmcp_schema.h
3 * @brief JSON Schema validator for tool inputs.
4 *
5 * cMCP's `tools/call` handler validates the incoming `arguments`
6 * object against the tool's declared `inputSchema` before dispatch.
7 * Validator surface near-parity with Ajv (the JSON Schema
8 * implementation the TypeScript MCP SDK uses); see
9 * `docs/schema-conformance.md` for the full keyword list and the
10 * documented deliberate departures (regex flavour, integer-vs-number
11 * distinction). Failures surface to the peer as JSON-RPC error
12 * -32602 with structured `{path, keyword, message}` data.
13 */
14#ifndef CMCP_SCHEMA_H
15#define CMCP_SCHEMA_H
16
17#include "cmcp_json.h"
18
19/* ====================================================================== */
20/* JSON Schema validator */
21/* ====================================================================== */
22/* Validates a `cmcp_json_t` value against a `cmcp_json_t` schema. The
23 * schema is itself a JSON document; it is parsed at tool-registration
24 * time and kept as a parsed tree so validation is allocation-light.
25 *
26 * Supported keywords (full list + semantics in docs/schema-conformance.md):
27 *
28 * type, enum, const
29 * minLength / maxLength / pattern on strings
30 * minimum / maximum / exclusive{Min,Max}imum on numbers
31 * multipleOf on numbers
32 * minItems / maxItems / uniqueItems on arrays
33 * items (single + tuple), prefixItems on arrays
34 * additionalItems on arrays
35 * properties, required on objects
36 * patternProperties, additionalProperties on objects
37 * propertyNames on objects
38 * minProperties / maxProperties on objects
39 * allOf / anyOf / oneOf / not combinators
40 * if / then / else conditional
41 * true / false boolean schemas
42 *
43 * Not yet implemented (silently accepted; landing post-6.7):
44 * $ref / $defs / definitions, format, dependentRequired/Schemas,
45 * contains, unevaluatedProperties/Items. See docs/schema-conformance.md
46 * for the deferred list and rationale. */
47
48typedef struct {
49 /* JSON Pointer (RFC 6901) into the offending value. "" = root. */
50 char *path;
51 /* Schema keyword that rejected the value, e.g. "type", "required". */
52 char *keyword;
53 /* Human-readable reason. */
54 char *message;
56
59
60/* Validate `value` against `schema`.
61 *
62 * schema must be a non-NULL JSON object. If it isn't, returns
63 * CMCP_EINVAL.
64 * value the value to check. May be NULL — treated as a JSON null.
65 * err OUT, optional. If non-NULL it is initialised by this call;
66 * on validation failure it is populated with the path,
67 * offending keyword, and a message. Caller frees with
68 * cmcp_schema_error_clear().
69 *
70 * Returns:
71 * CMCP_OK value matches the schema
72 * CMCP_ESCHEMA value violates the schema (err populated if requested)
73 * CMCP_EINVAL schema itself is malformed (NULL or not an object)
74 * CMCP_ENOMEM allocation failed during validation
75 */
76int cmcp_schema_validate(const cmcp_json_t *schema,
77 const cmcp_json_t *value,
79
80/* Convert a schema error to a JSON object suitable for the `data`
81 * field of a JSON-RPC -32602 INVALID_PARAMS response. The shape is:
82 *
83 * { "path": "/foo/3", "keyword": "type", "message": "..." }
84 *
85 * Caller owns the result. Returns NULL on allocation failure or if
86 * `e` has no populated fields. */
88
89#endif
Hand-rolled JSON value tree, parser, and emitter.
void cmcp_schema_error_init(cmcp_schema_error_t *e)
void cmcp_schema_error_clear(cmcp_schema_error_t *e)
cmcp_json_t * cmcp_schema_error_to_json(const cmcp_schema_error_t *e)
int cmcp_schema_validate(const cmcp_json_t *schema, const cmcp_json_t *value, cmcp_schema_error_t *err)