NAME

MCP::Server::KnowledgeStore::ResourceTemplates - MCP server support for resource templates and tool-call logging

VERSION

version 0.001

SYNOPSIS

use MCP::Server::KnowledgeStore::ResourceTemplates;

my $server = MCP::Server::KnowledgeStore::ResourceTemplates->new(name => 'my-server');

# Add templates
$server->add_template(
  uri_template => 'memory://memories/{name}',
  name => 'memory_read',
  description => 'Read a memory by name',
  mime_type => 'text/markdown',
  code => sub ($template, $params, $context) {
    my $name = $params->{name};
    # ... fetch and return content
    return "Content of memory $name";
  },
);

DESCRIPTION

This module extends MCP::Server with resource template support, allowing URI templates with parameters like memory://memories/{name} to be registered and matched against incoming resource read requests.

Resource templates are URIs with placeholder parameters in curly braces. When a client requests a resource with a matching URI pattern, the template's callback is invoked with the extracted parameters.

NAME

MCP::Server::KnowledgeStore::ResourceTemplates - MCP server with resource template support

EXAMPLES

Basic Template

$server->add_template(
  uri_template => 'file:///data/{file}',
  name => 'file_reader',
  description => 'Read files from the data directory',
  mime_type => 'text/plain',
  code => sub ($template, $params, $context) {
    my $file = $params->{file};
    # Validate and read file
    return "File content";
  },
);

Multi-parameter Template

$server->add_template(
  uri_template => 'memory://{kind}/{name}/revisions/{rev}',
  name => 'revision_reader',
  description => 'Read a specific revision',
  mime_type => 'text/markdown',
  code => sub ($template, $params, $context) {
    my ($kind, $name, $rev) = @{$params}{qw(kind name rev)};
    # Fetch and return content
  },
);

METHODS

add_template

Register a new resource template.

uri_template

Required. The URI template with parameter placeholders, e.g., memory://memories/{name} or memory://memories/{name}/revisions/{revision}.

Placeholder syntax: {param_name} where param_name matches /[a-zA-Z_][a-zA-Z0-9_]*/.

name

Required. The template name.

description

Optional. Human-readable description. Defaults to 'Resource template'.

mime_type

Optional. MIME type for responses. Defaults to 'text/plain'.

code

Required. Callback that receives ($template, $params, $context) and returns the resource content.

The $params argument is a hashref containing the extracted parameter values.

The callback should return either:

  • A hashref with contents key (standard MCP resource format)

  • A scalar (text string) which will be wrapped in text_resource format

  • undef (resource not found, will return 404)

SEE ALSO

MCP::Server, MCP::Resource, MCP::Server::KnowledgeStore::Tools.

AUTHOR

Wesley Schwengle <waterkip@cpan.org>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2026 by Wesley Schwengle.

This is free software, licensed under:

The (three-clause) BSD License