| 1 | package MCP::Server::KnowledgeStore; | ||
| 2 | 2 | ▶ | use v5.38; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 0 | ▶ | use Mojo::Base 'Mojolicious', -signatures; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 4 | |||
| 5 | our $VERSION = '0.001'; | ||
| 6 | |||
| 7 | # ABSTRACT: MCP server for shared memories, skills, specs, and agents | ||
| 8 | |||
| 9 | use Mojo::Util qw(secure_compare); | ||
| 10 | use MCP::Server::KnowledgeStore::Store; | ||
| 11 | use MCP::Server::KnowledgeStore::Tools; | ||
| 12 | |||
| 13 | has store => sub ($self) { | ||
| 14 | my $dsn = $ENV{MCP_KS_PG} // $self->config->{pg} | ||
| 15 | or die "MCP_KS_PG is not set: nothing to serve\n"; | ||
| 16 | return MCP::Server::KnowledgeStore::Store->new(pg => $dsn); | ||
| 17 | }; | ||
| 18 | |||
| 19 | # Exposed so a test (or anything else embedding this app) can register | ||
| 20 | # an extra tool after startup - MCP::Server looks tools up by name per | ||
| 21 | # request, not from a snapshot taken when to_action() was called, so | ||
| 22 | # this stays live. | ||
| 23 | has mcp_server => sub ($self) { | ||
| 24 | |||
| 25 | # Check if purge is disabled via environment variable | ||
| 26 | my $allow_purge = !exists $ENV{MCP_KS_NO_PURGE} || !$ENV{MCP_KS_NO_PURGE}; | ||
| 27 | return MCP::Server::KnowledgeStore::Tools->build($self->store, | ||
| 28 | allow_purge => $allow_purge,)->log($self->log); | ||
| 29 | }; | ||
| 30 | |||
| 31 | sub startup ($self) { | ||
| 32 | $self->plugin('Config' => { file => 'mcp-ks.conf' }) | ||
| 33 | if -f $self->home->child('mcp-ks.conf'); | ||
| 34 | |||
| 35 | push @{ $self->commands->namespaces }, | ||
| 36 | 'MCP::Server::KnowledgeStore::Command'; | ||
| 37 | |||
| 38 | # A bootstrap credential, for reaching a fresh deployment before any | ||
| 39 | # token has been minted. Unlike a real token it cannot be revoked from | ||
| 40 | # the database, so drop it once `token add` has been run. | ||
| 41 | my $bootstrap = $ENV{MCP_KS_TOKEN} // $self->config->{token}; | ||
| 42 | |||
| 43 | # Migrations run at startup, so a fresh database and an upgraded image | ||
| 44 | # both end up at the current schema without a separate deploy step. | ||
| 45 | $self->store->migrate unless $ENV{MCP_KS_NO_MIGRATE}; | ||
| 46 | |||
| 47 | my $server = $self->mcp_server; | ||
| 48 | my $routes = $self->routes; | ||
| 49 | |||
| 50 | # Agents authenticate with a bearer token and get the MCP tools. They | ||
| 51 | # have no accounts and no sessions: this is the only way in, and it is | ||
| 52 | # deliberately not shared with anything a human would log into. The | ||
| 53 | # transport answers a false return with a 401 challenge of its own. | ||
| 54 | my $action = $server->to_action( | ||
| 55 | { | ||
| 56 | auth => sub ($c) { | ||
| 57 | my $header = $c->req->headers->authorization // ''; | ||
| 58 | my ($sent) = $header =~ /\ABearer[ ]+(\S+)\z/; | ||
| 59 | return undef unless defined $sent; | ||
| 60 | |||
| 61 | # The token's name becomes the principal, so every revision it | ||
| 62 | # writes is attributed to the agent that presented it. | ||
| 63 | if (my $token = $self->store->verify_token($sent)) { | ||
| 64 | $c->stash('mcp.ks.principal' => $token->{name}); | ||
| 65 | return { principal => $token->{name} }; | ||
| 66 | } | ||
| 67 | |||
| 68 | return undef unless defined $bootstrap && length $bootstrap; | ||
| 69 | |||
| 70 | # Constant-time, so a wrong token leaks nothing through timing. | ||
| 71 | return undef unless secure_compare($sent, $bootstrap); | ||
| 72 | $c->stash('mcp.ks.principal' => 'bootstrap'); | ||
| 73 | return { principal => 'bootstrap' }; | ||
| 74 | }, | ||
| 75 | } | ||
| 76 | ); | ||
| 77 | $routes->any('/mcp')->to( | ||
| 78 | cb => sub ($c) { | ||
| 79 | my $result = $action->($c); | ||
| 80 | if (($c->res->code // 0) >= 400) { | ||
| 81 | my $request = eval { $c->req->json } // {}; | ||
| 82 | my $reply = eval { $c->res->json } // {}; | ||
| 83 | $server->log_transport_rejection($c->stash('mcp.ks.principal'), | ||
| 84 | $request, $reply); | ||
| 85 | } | ||
| 86 | return $result; | ||
| 87 | } | ||
| 88 | )->name('mcp'); | ||
| 89 | |||
| 90 | # Unauthenticated on purpose: this is what traefik and the container | ||
| 91 | # runtime poll, and it says nothing about the contents of the store. It | ||
| 92 | # does touch the database, since a server that cannot reach Postgres is | ||
| 93 | # not healthy however well it answers HTTP. | ||
| 94 | $routes->get('/health')->to( | ||
| 95 | cb => sub ($c) { | ||
| 96 | my $ok = eval { $self->store->db->query('SELECT 1'); 1 }; | ||
| 97 | $c->app->log->error("Health check failed: $@") unless $ok; | ||
| 98 | return $c->render( | ||
| 99 | status => $ok ? 200 : 503, | ||
| 100 | json => | ||
| 101 | { status => $ok ? 'ok' : 'error', database => $ok ? 'up' : 'down' }, | ||
| 102 | ); | ||
| 103 | } | ||
| 104 | )->name('health'); | ||
| 105 | |||
| 106 | return; | ||
| 107 | } | ||
| 108 | |||
| 109 | 1; | ||
| 110 | |||
| 111 | __END__ | ||