summaryrefslogtreecommitdiff
path: root/src/mcp
diff options
context:
space:
mode:
authortanaka takashi <isazaka@gmail.com>2026-07-01 00:11:08 +0900
committertanaka takashi <isazaka@gmail.com>2026-07-01 00:11:08 +0900
commita44fd4cf3a0c3d7ae33b7664341a0a9bff07aaaa (patch)
tree5c3152e7df8b16b207d6ebd69dfaf205765a8044 /src/mcp
parente3db01cd487c5b666366885367c8bfff4a6a09da (diff)
大規模履歴向けread_messages pagination対応
Diffstat (limited to 'src/mcp')
-rw-r--r--src/mcp/tools/common/context.c20
-rw-r--r--src/mcp/tools/common/context.h3
-rw-r--r--src/mcp/tools/common/history.c286
-rw-r--r--src/mcp/tools/common/history.h17
-rw-r--r--src/mcp/tools/read_messages.c298
-rw-r--r--src/mcp/tools/rebuild_index.c9
-rw-r--r--src/mcp/tools/write_message.c8
7 files changed, 614 insertions, 27 deletions
diff --git a/src/mcp/tools/common/context.c b/src/mcp/tools/common/context.c
index 1a469e0..d79b5de 100644
--- a/src/mcp/tools/common/context.c
+++ b/src/mcp/tools/common/context.c
@@ -42,8 +42,11 @@ tiny_mcp_tools_context_new(tiny_store_t *store)
}
context->store = store;
context->search_index = tiny_mcp_tools_build_search_index(store);
- if (!context->search_index)
+ context->history_index = tiny_history_index_build(store);
+ if (!context->search_index || !context->history_index)
{
+ tiny_search_index_free(context->search_index);
+ tiny_history_index_free(context->history_index);
free(context);
return NULL;
}
@@ -58,6 +61,7 @@ tiny_mcp_tools_context_free(tiny_mcp_tools_context_t *context)
return;
}
tiny_search_index_free(context->search_index);
+ tiny_history_index_free(context->history_index);
free(context);
}
@@ -86,3 +90,17 @@ tiny_mcp_tools_context_current(void)
{
return active_context;
}
+
+bool
+tiny_mcp_tools_ensure_history_index(tiny_mcp_tools_context_t *context)
+{
+ if (!context)
+ {
+ return false;
+ }
+ if (!context->history_index)
+ {
+ context->history_index = tiny_history_index_build(context->store);
+ }
+ return context->history_index != NULL;
+}
diff --git a/src/mcp/tools/common/context.h b/src/mcp/tools/common/context.h
index 0847d4b..2a6650a 100644
--- a/src/mcp/tools/common/context.h
+++ b/src/mcp/tools/common/context.h
@@ -2,12 +2,14 @@
#define TINY_CONVMEM_MCP_TOOLS_CONTEXT_H
#include "domain/search.h"
+#include "mcp/tools/common/history.h"
#include "storage/store.h"
typedef struct tiny_mcp_tools_context
{
tiny_store_t *store;
tiny_search_index_t *search_index;
+ tiny_history_index_t *history_index;
} tiny_mcp_tools_context_t;
tiny_mcp_tools_context_t *tiny_mcp_tools_context_new(tiny_store_t *store);
@@ -16,5 +18,6 @@ bool tiny_mcp_tools_context_activate(tiny_mcp_tools_context_t *context);
void tiny_mcp_tools_context_deactivate(tiny_mcp_tools_context_t *context);
tiny_mcp_tools_context_t *tiny_mcp_tools_context_current(void);
tiny_search_index_t *tiny_mcp_tools_build_search_index(const tiny_store_t *store);
+bool tiny_mcp_tools_ensure_history_index(tiny_mcp_tools_context_t *context);
#endif
diff --git a/src/mcp/tools/common/history.c b/src/mcp/tools/common/history.c
new file mode 100644
index 0000000..6254d8c
--- /dev/null
+++ b/src/mcp/tools/common/history.c
@@ -0,0 +1,286 @@
+#include "mcp/tools/common/history.h"
+
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "base/allocator.h"
+
+typedef struct history_bucket
+{
+ uint64_t hash;
+ char *session_id;
+ size_t *entries;
+ size_t count;
+ size_t capacity;
+} history_bucket_t;
+
+struct tiny_history_index
+{
+ history_bucket_t *buckets;
+ size_t count;
+ size_t capacity;
+ size_t *slots;
+ size_t slot_capacity;
+};
+
+#define EMPTY_SLOT SIZE_MAX
+
+static uint64_t
+session_hash(const char *value)
+{
+ uint64_t hash = UINT64_C(1469598103934665603);
+ while (*value)
+ {
+ hash ^= (unsigned char)*value++;
+ hash *= UINT64_C(1099511628211);
+ }
+ return hash;
+}
+
+static size_t
+start_slot(uint64_t hash, size_t capacity)
+{
+ return (size_t)(hash ^ (hash >> 32)) & (capacity - 1);
+}
+
+static history_bucket_t *
+find_bucket(const tiny_history_index_t *index, const char *session_id)
+{
+ uint64_t hash;
+ size_t slot, bucket;
+ if (!index || !session_id || !index->slot_capacity)
+ {
+ return NULL;
+ }
+ hash = session_hash(session_id);
+ slot = start_slot(hash, index->slot_capacity);
+ while ((bucket = index->slots[slot]) != EMPTY_SLOT)
+ {
+ if (bucket >= index->count)
+ {
+ return NULL;
+ }
+ if (index->buckets[bucket].hash == hash
+ && strcmp(index->buckets[bucket].session_id, session_id) == 0)
+ {
+ return &index->buckets[bucket];
+ }
+ slot = (slot + 1) & (index->slot_capacity - 1);
+ }
+ return NULL;
+}
+
+static void
+insert_slot(size_t *slots, size_t capacity, const history_bucket_t *buckets, size_t bucket)
+{
+ size_t slot = start_slot(buckets[bucket].hash, capacity);
+ while (slots[slot] != EMPTY_SLOT)
+ {
+ slot = (slot + 1) & (capacity - 1);
+ }
+ slots[slot] = bucket;
+}
+
+static bool
+reserve_table(tiny_history_index_t *index, size_t required)
+{
+ history_bucket_t *buckets;
+ size_t *slots, capacity, position;
+ if (required > index->capacity)
+ {
+ capacity = index->capacity ? index->capacity : 32;
+ while (capacity < required)
+ {
+ if (capacity > SIZE_MAX / 2)
+ {
+ return false;
+ }
+ capacity *= 2;
+ }
+ if (capacity > SIZE_MAX / sizeof(*buckets))
+ {
+ return false;
+ }
+ buckets = realloc(index->buckets, capacity * sizeof(*buckets));
+ if (!buckets)
+ {
+ return false;
+ }
+ index->buckets = buckets;
+ index->capacity = capacity;
+ }
+ capacity = index->slot_capacity ? index->slot_capacity : 64;
+ while (required > (capacity / 10) * 7)
+ {
+ if (capacity > SIZE_MAX / 2)
+ {
+ return false;
+ }
+ capacity *= 2;
+ }
+ if (capacity == index->slot_capacity)
+ {
+ return true;
+ }
+ slots = malloc(capacity * sizeof(*slots));
+ if (!slots)
+ {
+ return false;
+ }
+ for (position = 0; position < capacity; ++position)
+ {
+ slots[position] = EMPTY_SLOT;
+ }
+ for (position = 0; position < index->count; ++position)
+ {
+ insert_slot(slots, capacity, index->buckets, position);
+ }
+ free(index->slots);
+ index->slots = slots;
+ index->slot_capacity = capacity;
+ return true;
+}
+
+static int
+compare_entry(const tiny_store_t *store, size_t left, size_t right)
+{
+ const tiny_message_t *a = &tiny_store_entry(store, left)->message;
+ const tiny_message_t *b = &tiny_store_entry(store, right)->message;
+ if (a->timestamp_ms < b->timestamp_ms)
+ {
+ return -1;
+ }
+ if (a->timestamp_ms > b->timestamp_ms)
+ {
+ return 1;
+ }
+ return strcmp(a->message_id, b->message_id);
+}
+
+bool
+tiny_history_index_add(tiny_history_index_t *index, const tiny_store_t *store, size_t entry_index)
+{
+ const tiny_store_entry_t *entry = tiny_store_entry(store, entry_index);
+ history_bucket_t *bucket;
+ size_t low, high, capacity;
+ if (!index || !entry || !entry->message.session_id)
+ {
+ return false;
+ }
+ bucket = find_bucket(index, entry->message.session_id);
+ if (!bucket)
+ {
+ char *session = NULL;
+ size_t *entries = malloc(8 * sizeof(*entries));
+ size_t length = strlen(entry->message.session_id);
+ if (!entries || !(session = malloc(length + 1)) || index->count == SIZE_MAX
+ || !reserve_table(index, index->count + 1))
+ {
+ free(entries);
+ free(session);
+ return false;
+ }
+ memcpy(session, entry->message.session_id, length + 1);
+ bucket = &index->buckets[index->count];
+ memset(bucket, 0, sizeof(*bucket));
+ bucket->hash = session_hash(session);
+ bucket->session_id = session;
+ bucket->entries = entries;
+ bucket->capacity = 8;
+ insert_slot(index->slots, index->slot_capacity, index->buckets, index->count++);
+ }
+ if (bucket->count == bucket->capacity)
+ {
+ size_t *resized;
+ if (bucket->capacity > SIZE_MAX / 2
+ || (capacity = bucket->capacity * 2) > SIZE_MAX / sizeof(*resized)
+ || !(resized = realloc(bucket->entries, capacity * sizeof(*resized))))
+ {
+ return false;
+ }
+ bucket->entries = resized;
+ bucket->capacity = capacity;
+ }
+ low = 0;
+ high = bucket->count;
+ while (low < high)
+ {
+ size_t middle = low + (high - low) / 2;
+ if (compare_entry(store, bucket->entries[middle], entry_index) <= 0)
+ {
+ low = middle + 1;
+ }
+ else
+ {
+ high = middle;
+ }
+ }
+ memmove(bucket->entries + low + 1,
+ bucket->entries + low,
+ (bucket->count - low) * sizeof(*bucket->entries));
+ bucket->entries[low] = entry_index;
+ bucket->count++;
+ return true;
+}
+
+tiny_history_index_t *
+tiny_history_index_build(const tiny_store_t *store)
+{
+ tiny_history_index_t *index = calloc(1, sizeof(*index));
+ size_t position;
+ if (!index)
+ {
+ return NULL;
+ }
+ for (position = 0; position < tiny_store_entry_count(store); ++position)
+ {
+ if (!tiny_store_entry(store, position)->deleted
+ && !tiny_history_index_add(index, store, position))
+ {
+ tiny_history_index_free(index);
+ return NULL;
+ }
+ }
+ return index;
+}
+
+void
+tiny_history_index_free(tiny_history_index_t *index)
+{
+ size_t position;
+ if (!index)
+ {
+ return;
+ }
+ for (position = 0; position < index->count; ++position)
+ {
+ free(index->buckets[position].session_id);
+ free(index->buckets[position].entries);
+ }
+ free(index->buckets);
+ free(index->slots);
+ free(index);
+}
+
+bool
+tiny_history_index_session(const tiny_history_index_t *index,
+ const char *session_id,
+ const size_t **entries,
+ size_t *count)
+{
+ history_bucket_t *bucket;
+ if (!entries || !count)
+ {
+ return false;
+ }
+ *entries = NULL;
+ *count = 0;
+ bucket = find_bucket(index, session_id);
+ if (bucket)
+ {
+ *entries = bucket->entries;
+ *count = bucket->count;
+ }
+ return index && session_id;
+}
diff --git a/src/mcp/tools/common/history.h b/src/mcp/tools/common/history.h
new file mode 100644
index 0000000..ac08d99
--- /dev/null
+++ b/src/mcp/tools/common/history.h
@@ -0,0 +1,17 @@
+#ifndef TINY_CONVMEM_MCP_TOOLS_HISTORY_H
+#define TINY_CONVMEM_MCP_TOOLS_HISTORY_H
+
+#include "storage/store.h"
+
+typedef struct tiny_history_index tiny_history_index_t;
+
+tiny_history_index_t *tiny_history_index_build(const tiny_store_t *store);
+void tiny_history_index_free(tiny_history_index_t *index);
+bool
+tiny_history_index_add(tiny_history_index_t *index, const tiny_store_t *store, size_t entry_index);
+bool tiny_history_index_session(const tiny_history_index_t *index,
+ const char *session_id,
+ const size_t **entries,
+ size_t *count);
+
+#endif
diff --git a/src/mcp/tools/read_messages.c b/src/mcp/tools/read_messages.c
index 5a9e49f..1581534 100644
--- a/src/mcp/tools/read_messages.c
+++ b/src/mcp/tools/read_messages.c
@@ -3,25 +3,186 @@
#include "mcp/tools/common/common.h"
#include "mcp/tools/common/context.h"
+#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "base/allocator.h"
+#include "storage/codec.h"
+
+#define READ_DEFAULT_LIMIT 100
+#define READ_MAX_LIMIT 1000
+#define READ_RESPONSE_BUDGET (8U * 1024U * 1024U - 64U * 1024U)
+#define CURSOR_BYTES 47U
+#define CURSOR_TEXT_MAX 64U
+
+typedef struct history_cursor
+{
+ int64_t timestamp;
+ char message_id[27];
+} history_cursor_t;
+
+static uint64_t
+session_hash(const char *value)
+{
+ uint64_t hash = UINT64_C(1469598103934665603);
+ while (*value)
+ {
+ hash ^= (unsigned char)*value++;
+ hash *= UINT64_C(1099511628211);
+ }
+ return hash;
+}
+
+static size_t
+base64url_encode(const unsigned char *input, size_t length, char *output, size_t capacity)
+{
+ static const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
+ size_t source = 0, target = 0;
+ while (source < length)
+ {
+ uint32_t value = (uint32_t)input[source++] << 16;
+ unsigned available = 1;
+ if (source < length)
+ {
+ value |= (uint32_t)input[source++] << 8;
+ available++;
+ }
+ if (source < length)
+ {
+ value |= input[source++];
+ available++;
+ }
+ if (target + available + 1 >= capacity)
+ {
+ return 0;
+ }
+ output[target++] = alphabet[(value >> 18) & 63];
+ output[target++] = alphabet[(value >> 12) & 63];
+ if (available > 1)
+ {
+ output[target++] = alphabet[(value >> 6) & 63];
+ }
+ if (available > 2)
+ {
+ output[target++] = alphabet[value & 63];
+ }
+ }
+ output[target] = '\0';
+ return target;
+}
static int
-compare_history(const void *left, const void *right)
+base64_value(unsigned char value)
+{
+ if (value >= 'A' && value <= 'Z')
+ {
+ return value - 'A';
+ }
+ if (value >= 'a' && value <= 'z')
+ {
+ return value - 'a' + 26;
+ }
+ if (value >= '0' && value <= '9')
+ {
+ return value - '0' + 52;
+ }
+ if (value == '-')
+ {
+ return 62;
+ }
+ if (value == '_')
+ {
+ return 63;
+ }
+ return -1;
+}
+
+static bool
+base64url_decode(const char *input, unsigned char output[CURSOR_BYTES])
{
- const tiny_store_entry_t *a = *(const tiny_store_entry_t *const *)left;
- const tiny_store_entry_t *b = *(const tiny_store_entry_t *const *)right;
- if (a->message.timestamp_ms < b->message.timestamp_ms)
+ size_t length = strlen(input), source = 0, target = 0;
+ uint32_t accumulator = 0;
+ unsigned bits = 0;
+ if (length != 63)
+ {
+ return false;
+ }
+ while (source < length)
+ {
+ int value = base64_value((unsigned char)input[source++]);
+ if (value < 0)
+ {
+ return false;
+ }
+ accumulator = (accumulator << 6) | (uint32_t)value;
+ bits += 6;
+ if (bits >= 8)
+ {
+ bits -= 8;
+ if (target >= CURSOR_BYTES)
+ {
+ return false;
+ }
+ output[target++] = (unsigned char)(accumulator >> bits);
+ accumulator &= bits ? (UINT32_C(1) << bits) - 1 : 0;
+ }
+ }
+ return target == CURSOR_BYTES && accumulator == 0;
+}
+
+static bool
+cursor_encode(const char *session, const tiny_message_t *message, char output[CURSOR_TEXT_MAX])
+{
+ unsigned char data[CURSOR_BYTES] = { 0 };
+ data[0] = 1;
+ tiny_codec_put_u64(data + 1, session_hash(session));
+ tiny_codec_put_u64(data + 9, (uint64_t)message->timestamp_ms);
+ memcpy(data + 17, message->message_id, 26);
+ tiny_codec_put_u32(data + 43, tiny_codec_crc32(data, 43));
+ return base64url_encode(data, sizeof(data), output, CURSOR_TEXT_MAX) == 63;
+}
+
+static bool
+cursor_decode(const char *session, const char *value, history_cursor_t *cursor)
+{
+ unsigned char data[CURSOR_BYTES];
+ if (!value || !value[0] || !base64url_decode(value, data) || data[0] != 1
+ || tiny_codec_get_u64(data + 1) != session_hash(session)
+ || tiny_codec_get_u32(data + 43) != tiny_codec_crc32(data, 43))
+ {
+ return false;
+ }
+ memcpy(cursor->message_id, data + 17, 26);
+ cursor->message_id[26] = '\0';
+ cursor->timestamp = (int64_t)tiny_codec_get_u64(data + 9);
+ return cursor->timestamp >= 0 && tiny_message_id_valid(cursor->message_id);
+}
+
+static int
+compare_cursor(const tiny_message_t *message, const history_cursor_t *cursor)
+{
+ if (message->timestamp_ms < cursor->timestamp)
{
return -1;
}
- if (a->message.timestamp_ms > b->message.timestamp_ms)
+ if (message->timestamp_ms > cursor->timestamp)
{
return 1;
}
- return strcmp(a->message.message_id, b->message.message_id);
+ return strcmp(message->message_id, cursor->message_id);
+}
+
+static size_t
+escaped_size(const char *value, size_t length)
+{
+ size_t position, total = 0;
+ for (position = 0; position < length; ++position)
+ {
+ unsigned char byte = (unsigned char)value[position];
+ total += byte < 0x20 ? 6 : (byte == '"' || byte == '\\' ? 2 : 1);
+ }
+ return total;
}
static void
@@ -29,52 +190,139 @@ call_read_messages(const mcpc_tool_t *tool, mcpc_ucbr_t *result)
{
tiny_mcp_tools_context_t *context = tiny_mcp_tools_context_current();
char *session = tiny_mcp_tool_get_string(tool, "session_id", TINY_SESSION_ID_MAX);
- const tiny_store_entry_t **items = NULL;
- size_t index, count = 0;
+ char *cursor_text = tiny_mcp_tool_get_string(tool, "cursor", CURSOR_TEXT_MAX);
+ int32_t requested_limit = READ_DEFAULT_LIMIT;
+ mcpc_errcode_t limit_status
+ = mcpc_tool_get_tpropval_i32(tool, (const char8_t *)"limit", &requested_limit);
+ const size_t *entries = NULL;
+ size_t entry_count = 0, position = 0, emitted = 0, outer_size = 256;
+ history_cursor_t cursor;
+ bool has_cursor = cursor_text != NULL, more = false;
+ const tiny_message_t *last = NULL;
tiny_json_buffer_t json;
- if (!context || !session || !session[0])
+
+ if (!context || !session || !session[0]
+ || (limit_status != MCPC_EC_0 && limit_status != MCPC_EC_ANYDATA_NOTFOUND)
+ || requested_limit < 1 || requested_limit > READ_MAX_LIMIT
+ || (has_cursor && !cursor_decode(session, cursor_text, &cursor)))
{
+ tiny_mcp_tool_add_error(result, "VALIDATION_ERROR", "invalid read_messages arguments");
free(session);
- tiny_mcp_tool_add_error(result, "VALIDATION_ERROR", "session_id is required");
+ free(cursor_text);
return;
}
- items = malloc((tiny_store_entry_count(context->store) + 1) * sizeof(*items));
- if (!items)
+ if (!tiny_mcp_tools_ensure_history_index(context)
+ || !tiny_history_index_session(context->history_index, session, &entries, &entry_count))
{
+ tiny_mcp_tool_add_error(result, "INTERNAL_ERROR", "history index allocation failed");
free(session);
- tiny_mcp_tool_add_error(result, "INTERNAL_ERROR", "out of memory");
+ free(cursor_text);
return;
}
- for (index = 0; index < tiny_store_entry_count(context->store); ++index)
+ if (has_cursor)
{
- const tiny_store_entry_t *entry = tiny_store_entry(context->store, index);
- if (!entry->deleted && strcmp(entry->message.session_id, session) == 0)
+ size_t low = 0, high = entry_count;
+ while (low < high)
{
- items[count++] = entry;
+ size_t middle = low + (high - low) / 2;
+ const tiny_store_entry_t *entry = tiny_store_entry(context->store, entries[middle]);
+ if (compare_cursor(&entry->message, &cursor) <= 0)
+ {
+ low = middle + 1;
+ }
+ else
+ {
+ high = middle;
+ }
}
+ position = low;
}
- qsort(items, count, sizeof(*items), compare_history);
+
tiny_json_init(&json);
tiny_json_raw(&json, "{\"history\":[");
- for (index = 0; index < count; ++index)
+ while (position < entry_count && emitted < (size_t)requested_limit)
{
- if (index)
+ const tiny_store_entry_t *entry = tiny_store_entry(context->store, entries[position]);
+ tiny_json_buffer_t item;
+ size_t item_size;
+ if (!entry || entry->deleted)
+ {
+ position++;
+ continue;
+ }
+ tiny_json_init(&item);
+ tiny_mcp_tool_append_message(&item, &entry->message);
+ item_size = escaped_size(item.data, item.length) + (emitted ? 2 : 0);
+ if (item.failed || (emitted && outer_size + item_size > READ_RESPONSE_BUDGET))
+ {
+ tiny_json_free(&item);
+ if (item.failed)
+ {
+ json.failed = true;
+ }
+ more = true;
+ break;
+ }
+ if (emitted)
{
tiny_json_raw(&json, ",");
}
- tiny_mcp_tool_append_message(&json, &items[index]->message);
+ tiny_json_raw_n(&json, item.data, item.length);
+ outer_size += item_size;
+ emitted++;
+ last = &entry->message;
+ position++;
+ tiny_json_free(&item);
+ }
+ if (!more)
+ {
+ size_t lookahead = position;
+ while (lookahead < entry_count)
+ {
+ const tiny_store_entry_t *entry = tiny_store_entry(context->store, entries[lookahead++]);
+ if (entry && !entry->deleted)
+ {
+ more = true;
+ break;
+ }
+ }
+ }
+ tiny_json_raw(&json, "]");
+ if (more && last)
+ {
+ char next_cursor[CURSOR_TEXT_MAX];
+ if (!cursor_encode(session, last, next_cursor))
+ {
+ json.failed = true;
+ }
+ else
+ {
+ tiny_json_raw(&json, ",\"nextCursor\":");
+ tiny_json_string(&json, next_cursor);
+ }
+ }
+ tiny_json_raw(&json, "}");
+ if (json.failed)
+ {
+ tiny_mcp_tool_add_error(result, "INTERNAL_ERROR", "response allocation failed");
+ }
+ else
+ {
+ tiny_mcp_tool_add_json(result, &json);
}
- tiny_json_raw(&json, "]}");
- tiny_mcp_tool_add_json(result, &json);
tiny_json_free(&json);
- free(items);
free(session);
+ free(cursor_text);
}
bool
tiny_mcp_register_read_messages(mcpc_server_t *server)
{
- mcpc_tool_t *tool = tiny_mcp_tool_new("read_messages", "Read a session", call_read_messages);
+ mcpc_tool_t *tool = tiny_mcp_tool_new("read_messages", "Read a session page", call_read_messages);
return tool && tiny_mcp_tool_add_property(tool, "session_id", "Session ID", MCPC_U8STR, true)
+ && tiny_mcp_tool_add_property(
+ tool, "limit", "Optional page limit from 1 to 1000; defaults to 100", MCPC_I32, false)
+ && tiny_mcp_tool_add_property(
+ tool, "cursor", "Optional opaque page cursor", MCPC_U8STR, false)
&& mcpc_server_add_tool(server, tool) == MCPC_EC_0;
}
diff --git a/src/mcp/tools/rebuild_index.c b/src/mcp/tools/rebuild_index.c
index 69f8341..7208d77 100644
--- a/src/mcp/tools/rebuild_index.c
+++ b/src/mcp/tools/rebuild_index.c
@@ -11,6 +11,7 @@ call_rebuild_index(const mcpc_tool_t *tool, mcpc_ucbr_t *result)
tiny_mcp_tools_context_t *context = tiny_mcp_tools_context_current();
tiny_rebuild_report_t report;
tiny_search_index_t *rebuilt;
+ tiny_history_index_t *history;
tiny_store_status_t status;
tiny_json_buffer_t json;
(void)tool;
@@ -25,9 +26,14 @@ call_rebuild_index(const mcpc_tool_t *tool, mcpc_ucbr_t *result)
tiny_mcp_tool_add_error(result, tiny_store_status_name(status), "index rebuild failed");
return;
}
+ tiny_history_index_free(context->history_index);
+ context->history_index = NULL;
rebuilt = tiny_mcp_tools_build_search_index(context->store);
- if (!rebuilt)
+ history = tiny_history_index_build(context->store);
+ if (!rebuilt || !history)
{
+ tiny_search_index_free(rebuilt);
+ tiny_history_index_free(history);
tiny_search_index_free(context->search_index);
context->search_index = NULL;
tiny_mcp_tool_add_error(result, "INTERNAL_ERROR", "search index allocation failed");
@@ -35,6 +41,7 @@ call_rebuild_index(const mcpc_tool_t *tool, mcpc_ucbr_t *result)
}
tiny_search_index_free(context->search_index);
context->search_index = rebuilt;
+ context->history_index = history;
tiny_json_init(&json);
tiny_json_raw(&json, "{\"status\":\"OK\",\"messages_indexed\":");
tiny_json_u64(&json, report.messages_indexed);
diff --git a/src/mcp/tools/write_message.c b/src/mcp/tools/write_message.c
index f9c078a..c4b18cc 100644
--- a/src/mcp/tools/write_message.c
+++ b/src/mcp/tools/write_message.c
@@ -185,6 +185,14 @@ call_write_message(const mcpc_tool_t *tool, mcpc_ucbr_t *result)
tiny_search_index_free(context->search_index);
context->search_index = NULL;
}
+ if (context->history_index
+ && !tiny_history_index_add(context->history_index,
+ context->store,
+ tiny_store_entry_count(context->store) - 1))
+ {
+ tiny_history_index_free(context->history_index);
+ context->history_index = NULL;
+ }
tiny_json_init(&json);
tiny_json_raw(&json, "{\"status\":\"OK\",\"message\":");
tiny_mcp_tool_append_message(&json, &written);