Pyjo.MCP.ResourceTemplate - Resource template of a server of MCP

from Pyjo.MCP.ResourceTemplate import ResourceTemplate

def note(number: str) -> str:
    """A note, by its number."""
    return f'Note {number}'

template = ResourceTemplate('notes://{number}', note)
print(template.uri, template.name, template.description)

# Or import the module, as in Google Python Style Guide

import Pyjo.MCP.ResourceTemplate

template = Pyjo.MCP.ResourceTemplate.new('file:///project/{+path}', lambda path: path.upper())
template.resource('file:///project/README.md').read_p().then(print).wait()

Pyjo.MCP.ResourceTemplate is a resource template of Pyjo.MCP.Server, the resources of the URIs that a URI Template of RFC 6570 of Pyjo.URL.Template matches, which a client reads by their URIs. The values of the variables of a URI are the keyword arguments of the function, which returns the contents of the resource, as the function of a Pyjo.MCP.Resource does. The name and the description of the resource template are those of the function by default: its name and the first paragraph of its docstring.

The values of the variables come from the client, so a URI whose value has a segment .., an absolute path or the character NUL, which would read a file outside of a directory of the function, has no resource, unless the resource template is not safe.

See also

Pyjo.MCP.Resource, Pyjo.MCP.Server, Pyjo.URL.Template, Pyjo.MCP.

Classes

class Pyjo.MCP.ResourceTemplate.Pyjo_MCP_ResourceTemplate(uri: str, cb: Callable[[...], Any], *, name: str | None = None, title: str | None = None, description: str | None = None, mime_type: str = 'text/plain', context: bool = False, safe: bool = True, complete: Mapping[str, Any] | None = None)[source]

Pyjo.MCP.ResourceTemplate inherits all methods from Pyjo.Base and implements the following new ones.

__init__(uri: str, cb: Callable[[...], Any], *, name: str | None = None, title: str | None = None, description: str | None = None, mime_type: str = 'text/plain', context: bool = False, safe: bool = True, complete: Mapping[str, Any] | None = None) → None[source]
template = Pyjo.MCP.ResourceTemplate.new('notes://{number}', note)
template = Pyjo.MCP.ResourceTemplate.new('file:///project/{+path}', read, mime_type='text/markdown')
template = Pyjo.MCP.ResourceTemplate.new('users://{id}', lambda context, id: ..., context=True)
template = Pyjo.MCP.ResourceTemplate.new('notes://{number}', note, complete={'number': ['1', '2']})

Construct a new Pyjo.MCP.ResourceTemplate object of a URI Template and a function. With context, the function gets the Context of the request before the values of the variables. A URI Template that is not valid, or whose expressions cannot be told apart, raises a Pyjo.Exception.

__json__() → dict[str, Any][source]
definition = template.__json__()

The definition of the resource template, as resources/templates/list gives it, with its uriTemplate, name, title, description and mimeType, where it has them.

__weakref__

list of weak references to the object

cb
cb = template.cb

The function of the resource template.

complete: dict[str, Any]
completions = template.complete

The completions of the values of the variables, by their names, which completion/complete gives: a list of the values, which the server filters by what the user has typed, or a function that gets it, and the values of the other variables, after the context of the request with context, and returns the values, or a Pyjo.Promise of them.

template.complete['number'] = lambda value, arguments: [str(n) for n in range(1, len(notes) + 1)]
context
boolean = template.context

Whether the function gets the context of the request.

description
description = template.description

What the resources are, the first paragraph of the docstring of the function by default, or None.

match(uri: str) → dict[str, Any] | None[source]
variables = template.match('notes://1')

The values of the variables of a URI, as match() of the template gives them, or None for a URI that the template does not match, or whose value is not safe.

files = Pyjo.MCP.ResourceTemplate.new('file:///project/{+path}', lambda path: '')

# {'path': 'docs/index.md'}
files.match('file:///project/docs/index.md')

# None
files.match('file:///project/../etc/passwd')
mime_type
type = template.mime_type

The media type of the contents, text/plain by default.

name
name = template.name

The name of the resource template, the one of the function by default.

resource(uri: str) → Pyjo_MCP_Resource | None[source]
resource = template.resource('notes://1')

The Pyjo.MCP.Resource of a URI, whose function is the one of the resource template with the values of the variables of the URI, or None for a URI that match() does not match, or whose values the function does not take, as without a value of a variable that the function requires.

# {'contents': [{'uri': 'notes://1', 'mimeType': 'text/plain', 'text': 'Note 1'}]}
Pyjo.MCP.ResourceTemplate.new('notes://{number}', note).resource('notes://1').read_p().then(print).wait()
safe
boolean = template.safe

Whether a URI whose value has a segment .., an absolute path or the character NUL has no resource, true by default.

template
tpl = template.template

The Pyjo.URL.Template object of the URI Template.

title
title = template.title

The name of the resource template for people, or None.

uri
uri = template.uri

The URI Template of the resources, which resources/templates/list gives as uriTemplate.