summaryrefslogtreecommitdiff
path: root/src/mcp/tools/delete_message.c
blob: 9f708665b19f3ee4ca0d2d336fc078a51a4d64d2 (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
#include "mcp/tools/common/registry.h"

#include "mcp/tools/common/common.h"
#include "mcp/tools/common/context.h"

#include <stdlib.h>

#include "base/allocator.h"

static void
call_delete_message(const mcpc_tool_t *tool, mcpc_ucbr_t *result)
{
  tiny_mcp_tools_context_t *context = tiny_mcp_tools_context_current();
  char *message_id = tiny_mcp_tool_get_string(tool, "message_id", TINY_MESSAGE_ID_LENGTH);
  char *session = tiny_mcp_tool_get_string(tool, "session_id", TINY_SESSION_ID_MAX);
  tiny_store_status_t status;
  if (!context || !message_id || !tiny_message_id_valid(message_id))
    {
      tiny_mcp_tool_add_error(
          result, "VALIDATION_ERROR", "message_id must be a canonical 26-character ULID");
    }
  else
    {
      status
          = tiny_store_delete(context->store, message_id, session && session[0] ? session : NULL);
      if (status != TINY_STORE_OK && status != TINY_STORE_NOT_FOUND)
        {
          tiny_mcp_tool_add_error(
              result, tiny_store_status_name(status), "delete record was not written");
        }
      else
        {
          if (status == TINY_STORE_OK)
            {
              tiny_mcp_tools_invalidate_indexes(context);
            }
          mcpc_toolcall_result_add_text_printf8(
              result,
              (const char8_t *)(status == TINY_STORE_OK ? "{\"status\":\"OK\"}"
                                                        : "{\"status\":\"NOT_FOUND\"}"));
        }
    }
  free(message_id);
  free(session);
}

bool
tiny_mcp_register_delete_message(mcpc_server_t *server)
{
  mcpc_tool_t *tool = tiny_mcp_tool_new(
      "delete_message",
      "Delete a persisted message so it is immediately excluded from reads and "
      "searches. Physical removal occurs after rebuild_index compacts the store.",
      call_delete_message);
  return tool
         && tiny_mcp_tool_add_property(tool,
                                       "message_id",
                                       "Canonical 26-character ULID of the message to delete.",
                                       MCPC_U8STR,
                                       true)
         && tiny_mcp_tool_add_property(tool,
                                       "session_id",
                                       "Optional session guard; when provided, delete only if the "
                                       "message belongs to this session.",
                                       MCPC_U8STR,
                                       false)
         && mcpc_server_add_tool(server, tool) == MCPC_EC_0;
}