Pyjo.MCP.Prompt - Prompt of a server of MCP¶
from Pyjo.MCP.Prompt import Prompt
def review(code: str, language: str = 'Python') -> str:
"""Review a piece of code.
:param code: The code to review.
:param language: The language of the code.
"""
return f'Review this {language} code:\n{code}'
prompt = Prompt(review)
print(prompt.name, prompt.description, prompt.arguments)
# Or import the module, as in Google Python Style Guide
import Pyjo.MCP.Prompt
prompt = Pyjo.MCP.Prompt.new(review, name='code_review', title='Code review')
prompt.get_p({'code': 'x = 1'}).then(print).wait()
Pyjo.MCP.Prompt is a prompt of Pyjo.MCP.Server, a template of
messages for a language model, which a user picks and fills with its
arguments. The name, the description and the arguments of the prompt are those
of the function by default: its name, the first paragraph of its docstring,
and its parameters, with the descriptions of its docstring, of which those
without a default are required.
The function returns the messages of the prompt, as a value, a
Pyjo.Promise of it or a coroutine, of an async def:
a string is one message of the user with the text;
a
listis the messages, each with itsroleand itscontent;a
dictwithmessagesis the result as it is, as with adescriptionof its own.
A missing argument that is required, or one that the function does not take,
is an error of the request, -32602, as the specification requires, and an
exception of the function an error of the server.
See also¶
Pyjo.MCP.Server, Pyjo.MCP.Resource, Pyjo.MCP.Tool,
Pyjo.MCP.
Classes¶
- class Pyjo.MCP.Prompt.Pyjo_MCP_Prompt(cb: Callable[[...], Any], *, name: str | None = None, title: str | None = None, description: str | None = None, arguments: list[Mapping[str, Any]] | None = None, context: bool = False, complete: Mapping[str, Any] | None = None)[source]¶
Pyjo.MCP.Promptinherits all methods fromPyjo.Baseand implements the following new ones.- __init__(cb: Callable[[...], Any], *, name: str | None = None, title: str | None = None, description: str | None = None, arguments: list[Mapping[str, Any]] | None = None, context: bool = False, complete: Mapping[str, Any] | None = None) None[source]¶
prompt = Pyjo.MCP.Prompt.new(review) prompt = Pyjo.MCP.Prompt.new(review, name='code_review', title='Code review') prompt = Pyjo.MCP.Prompt.new(lambda **args: ..., arguments=[{'name': 'code', 'required': True}]) prompt = Pyjo.MCP.Prompt.new(lambda context, code: ..., context=True) prompt = Pyjo.MCP.Prompt.new(review, complete={'language': ['Perl', 'Python']})
Construct a new
Pyjo.MCP.Promptobject of a function. Withcontext, the function gets theContextof the request before its arguments.
- __json__() dict[str, Any][source]¶
definition = prompt.__json__()
The definition of the prompt, as
prompts/listgives it, with itsname,title,descriptionandarguments, where it has them.
- __weakref__¶
list of weak references to the object
- arguments: list[dict[str, Any]]¶
arguments = prompt.arguments
The arguments of the prompt, each with its
name, itsdescriptionand whether it isrequired, the parameters of the function by default.
- cb¶
cb = prompt.cb
The function of the prompt.
- complete: dict[str, Any]¶
completions = prompt.complete
The completions of the values of the arguments, by their names, which
completion/completegives: 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 arguments, after the context of the request withcontext, and returns the values, or aPyjo.Promiseof them. The values of a parameter withLiteralorEnumcomplete by default.prompt.complete['language'] = lambda value, arguments: ['Perl', 'Python']
- context¶
boolean = prompt.context
Whether the function gets the context of the request before its arguments.
- description¶
description = prompt.description
What the prompt is for, the first paragraph of the docstring of the function by default.
- get_p(arguments: Mapping[str, Any], context: Any = None) Pyjo_Promise[source]¶
promise = prompt.get_p({'code': 'x = 1'}) promise = prompt.get_p({'code': 'x = 1'}, context)
Call the function with the arguments, and return a
Pyjo.Promiseof the result of the prompt, asprompts/getgives it, without itsresultType, or rejected with aPyjo.JSON.RPC.Errorof arguments that are not valid, or 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.# {'messages': [{'role': 'user', 'content': {'type': 'text', 'text': 'Review this Python code:\nx = 1'}}]} prompt.get_p({'code': 'x = 1'}).then(print).wait()
- name¶
name = prompt.name
The name of the prompt, the one of the function by default.
- result(value: Any = None) dict[str, Any][source]¶
result = prompt.result('Review this code') result = prompt.result([{'role': 'assistant', 'content': {'type': 'text', 'text': 'Done'}}])
The result of the prompt of what the function returns.
- title¶
title = prompt.title
The name of the prompt for people, or
None.