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

from Pyjo.MCP.Resource import Resource

def readme() -> str:
    """The documentation of the project."""
    return '# Pyjo-MCP'

resource = Resource('file:///project/README.md', readme, mime_type='text/markdown')
print(resource.uri, resource.name, resource.description)

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

import Pyjo.MCP.Resource

resource = Pyjo.MCP.Resource.new('file:///project/logo.png', lambda: b'\x89PNG', mime_type='image/png')
resource.read_p().then(print).wait()

Pyjo.MCP.Resource is a resource of Pyjo.MCP.Server, data that a client reads by its URI, as the context of a language model. The name and the description of the resource are those of the function by default: its name and the first paragraph of its docstring.

The function returns the contents of the resource, as a value, a Pyjo.Promise of it or a coroutine, of an async def:

  • a string is the text of the contents;

  • bytes are the blob of the contents, in Base64;

  • a dict with contents is the result as it is, as with several of them;

  • another value is the text of its JSON.

See also

Pyjo.MCP.Server, Pyjo.MCP.Prompt, Pyjo.MCP.Tool, Pyjo.MCP.

Classes

class Pyjo.MCP.Resource.Pyjo_MCP_Resource(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)[source]

Pyjo.MCP.Resource 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) → None[source]
resource = Pyjo.MCP.Resource.new('file:///project/README.md', readme)
resource = Pyjo.MCP.Resource.new('file:///project/README.md', readme, mime_type='text/markdown')
resource = Pyjo.MCP.Resource.new('config://app', lambda context: ..., context=True)

Construct a new Pyjo.MCP.Resource object of a function. With context, the function gets the Context of the request.

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

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

__weakref__

list of weak references to the object

cb
cb = resource.cb

The function of the resource.

context
boolean = resource.context

Whether the function gets the context of the request.

description
description = resource.description

What the resource is, the first paragraph of the docstring of the function by default, or None.

mime_type
type = resource.mime_type

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

name
name = resource.name

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

read_p(context: Any = None) → Pyjo_Promise[source]
promise = resource.read_p()
promise = resource.read_p(context)

Call the function, and return a Pyjo.Promise of the result of the resource, as resources/read gives it, without its resultType, or rejected with an exception of the function. A promise or a coroutine of the function is cancelled with the Pyjo.MCP.Server.Context of the request, as Pyjo.MCP.Server.Context.cancellable() does.

# {'contents': [{'uri': 'file:///project/README.md', 'mimeType': 'text/markdown', 'text': '# Pyjo-MCP'}]}
resource.read_p().then(print).wait()
result(value: Any = None) → dict[str, Any][source]
result = resource.result('# Pyjo-MCP')
result = resource.result(b'\x89PNG')

The result of the resource of what the function returns.

title
title = resource.title

The name of the resource for people, or None.

uri
uri = resource.uri

The URI of the resource, which resources/read asks for.