1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
#include "mcp/tools/common/common.h"
#include <stdlib.h>
#include "base/allocator.h"
void
tiny_mcp_tool_add_error(mcpc_ucbr_t *result, const char *code, const char *message)
{
tiny_json_buffer_t json;
tiny_json_init(&json);
tiny_json_raw(&json, "{\"status\":\"ERROR\",\"error\":{\"code\":");
tiny_json_string(&json, code);
tiny_json_raw(&json, ",\"message\":");
tiny_json_string(&json, message);
tiny_json_raw(&json, "}}");
mcpc_toolcall_result_add_errmsg_printf8(
result, (const char8_t *)"%s", json.data ? json.data : "{}");
tiny_json_free(&json);
}
void
tiny_mcp_tool_add_json(mcpc_ucbr_t *result, tiny_json_buffer_t *json)
{
if (json->failed)
{
tiny_mcp_tool_add_error(result, "INTERNAL_ERROR", "response allocation failed");
return;
}
mcpc_toolcall_result_add_text_printf8(
result, (const char8_t *)"%s", json->data ? json->data : "{}");
}
char *
tiny_mcp_tool_get_string(const mcpc_tool_t *tool, const char *name, size_t maximum)
{
char *value = malloc(maximum + 1);
size_t length = 0;
if (!value
|| mcpc_tool_get_tpropval_u8str(
tool, (const char8_t *)name, (char8_t *)value, maximum + 1, &length)
!= MCPC_EC_0
|| length > maximum)
{
free(value);
return NULL;
}
value[length] = '\0';
if (!tiny_utf8_valid(value, length))
{
free(value);
return NULL;
}
return value;
}
void
tiny_mcp_tool_append_message(tiny_json_buffer_t *json, const tiny_message_t *message)
{
size_t index;
tiny_json_raw(json, "{\"message_id\":");
tiny_json_string(json, message->message_id);
tiny_json_raw(json, ",\"session_id\":");
tiny_json_string(json, message->session_id);
tiny_json_raw(json, ",\"timestamp\":");
tiny_json_i64(json, message->timestamp_ms);
tiny_json_raw(json, ",\"role\":");
tiny_json_string(json, tiny_role_name(message->role));
tiny_json_raw(json, ",\"content\":");
tiny_json_string(json, message->content);
tiny_json_raw(json, ",\"revision\":");
tiny_json_u64(json, message->revision);
tiny_json_raw(json, ",\"updated_at\":");
tiny_json_i64(json, message->updated_at_ms);
tiny_json_raw(json, ",\"metadata\":{");
if (message->metadata.tool_call_id)
{
tiny_json_raw(json, "\"tool_call_id\":");
tiny_json_string(json, message->metadata.tool_call_id);
tiny_json_raw(json, ",");
}
tiny_json_raw(json, "\"tags\":[");
for (index = 0; index < message->metadata.tag_count; ++index)
{
if (index)
{
tiny_json_raw(json, ",");
}
tiny_json_string(json, message->metadata.tags[index]);
}
tiny_json_raw(json, "],\"memory_tier\":");
tiny_json_string(json, tiny_tier_name(message->metadata.memory_tier));
tiny_json_raw(json, ",\"memory_kind\":");
tiny_json_string(json, tiny_kind_name(message->metadata.memory_kind));
tiny_json_raw(json, ",\"importance\":");
tiny_json_u64(json, message->metadata.importance);
if (message->metadata.summary)
{
tiny_json_raw(json, ",\"summary\":");
tiny_json_string(json, message->metadata.summary);
}
tiny_json_raw(json, ",\"relations\":[");
for (index = 0; index < message->metadata.relation_count; ++index)
{
if (index)
{
tiny_json_raw(json, ",");
}
tiny_json_raw(json, "{\"type\":");
tiny_json_string(json, tiny_relation_type_name(message->metadata.relations[index].type));
tiny_json_raw(json, ",\"message_id\":");
tiny_json_string(json, message->metadata.relations[index].message_id);
tiny_json_raw(json, "}");
}
tiny_json_raw(json, "],\"source_message_ids\":[");
for (index = 0; index < message->metadata.source_count; ++index)
{
if (index)
{
tiny_json_raw(json, ",");
}
tiny_json_string(json, message->metadata.source_message_ids[index]);
}
tiny_json_raw(json, "]");
tiny_json_raw(json, "}}");
}
mcpc_tool_t *
tiny_mcp_tool_new(const char *name, const char *description, mcpc_tcallcb_t callback)
{
mcpc_tool_t *tool = mcpc_tool_new2((const char8_t *)name, (const char8_t *)description);
if (!tool || mcpc_tool_set_call_cb(tool, callback) != MCPC_EC_0)
{
return NULL;
}
return tool;
}
bool
tiny_mcp_tool_add_property(mcpc_tool_t *tool,
const char *name,
const char *description,
mcpc_anytype_t type,
bool required)
{
mcpc_toolprop_t *property
= mcpc_toolprop_new2((const char8_t *)name, (const char8_t *)description, type);
if (!property)
{
return false;
}
if (mcpc_toolprop_set_required(property, required) != MCPC_EC_0)
{
return false;
}
mcpc_tool_add_toolprop(tool, property);
return true;
}
|