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 list is the messages, each with its role and its content;

  • a dict with messages is the result as it is, as with a description of 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.Prompt inherits all methods from Pyjo.Base and 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.Prompt object of a function. With context, the function gets the Context of the request before its arguments.

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

The definition of the prompt, as prompts/list gives it, with its name, title, description and arguments, 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, its description and whether it is required, 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/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 arguments, after the context of the request with context, and returns the values, or a Pyjo.Promise of them. The values of a parameter with Literal or Enum complete 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.Promise of the result of the prompt, as prompts/get gives it, without its resultType, or rejected with a Pyjo.JSON.RPC.Error of arguments that are not valid, or 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.

# {'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.

Pyjo.MCP.Prompt.text_result(text: str, role: str = 'user') → dict[str, Any][source]
result = Pyjo.MCP.Prompt.text_result('Review this code')
result = Pyjo.MCP.Prompt.text_result('Here is the review', role='assistant')

A result of a prompt with one message of the text, of the user by default.