blob: 1a469e0ce57a50ca78ca5ee802b5367bc3ee9183 (
plain)
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
|
#include "mcp/tools/common/context.h"
#include <stdlib.h>
#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;
}
|