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
textof the contents;bytesare theblobof the contents, in Base64;a
dictwithcontentsis the result as it is, as with several of them;another value is the text of its JSON.
See also¶
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.Resourceinherits all methods fromPyjo.Baseand 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.Resourceobject of a function. Withcontext, the function gets theContextof the request.
- __json__() dict[str, Any][source]¶
definition = resource.__json__()
The definition of the resource, as
resources/listgives it, with itsuri,name,title,descriptionandmimeType, 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/plainby 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.Promiseof the result of the resource, asresources/readgives it, without itsresultType, or rejected with an exception of the function. A promise or a coroutine of the function is cancelled with thePyjo.MCP.Server.Contextof the request, asPyjo.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/readasks for.