From 4bc721c3bf7589b519c5730d093f704ce56e3f09 Mon Sep 17 00:00:00 2001 From: tanaka takashi Date: Mon, 29 Jun 2026 21:43:54 +0900 Subject: refactor: MCP層をmcpc APIベースに刷新 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 独自JSON-RPC処理をmcpcへ置き換え - 各MCPツールを個別ソースへ分離 - 共通コンテキストと登録処理を整理 - mcpc互換修正と統合テストを更新 --- src/mcp/tools/common/context.c | 88 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/mcp/tools/common/context.c (limited to 'src/mcp/tools/common/context.c') diff --git a/src/mcp/tools/common/context.c b/src/mcp/tools/common/context.c new file mode 100644 index 0000000..1a469e0 --- /dev/null +++ b/src/mcp/tools/common/context.c @@ -0,0 +1,88 @@ +#include "mcp/tools/common/context.h" + +#include + +#include "base/allocator.h" + +/* mcpc's callback API has no user-data parameter. The stdio server is single-instance. */ +static tiny_mcp_tools_context_t *active_context; + +tiny_search_index_t * +tiny_mcp_tools_build_search_index(const tiny_store_t *store) +{ + tiny_search_index_t *index = tiny_search_index_create(); + size_t position; + if (!index) + { + return NULL; + } + for (position = 0; position < tiny_store_entry_count(store); ++position) + { + if (!tiny_search_index_add(index, &tiny_store_entry(store, position)->message, position)) + { + tiny_search_index_free(index); + return NULL; + } + } + return index; +} + +tiny_mcp_tools_context_t * +tiny_mcp_tools_context_new(tiny_store_t *store) +{ + tiny_mcp_tools_context_t *context; + if (!store) + { + return NULL; + } + context = malloc(sizeof(*context)); + if (!context) + { + return NULL; + } + context->store = store; + context->search_index = tiny_mcp_tools_build_search_index(store); + if (!context->search_index) + { + free(context); + return NULL; + } + return context; +} + +void +tiny_mcp_tools_context_free(tiny_mcp_tools_context_t *context) +{ + if (!context) + { + return; + } + tiny_search_index_free(context->search_index); + free(context); +} + +bool +tiny_mcp_tools_context_activate(tiny_mcp_tools_context_t *context) +{ + if (!context || active_context) + { + return false; + } + active_context = context; + return true; +} + +void +tiny_mcp_tools_context_deactivate(tiny_mcp_tools_context_t *context) +{ + if (active_context == context) + { + active_context = NULL; + } +} + +tiny_mcp_tools_context_t * +tiny_mcp_tools_context_current(void) +{ + return active_context; +} -- cgit v1.2.3