duck.contrib.mcp.decorators

Decorators for MCPView.

Module Contents

Functions

get_summary_from_docstring

Extract a short description from a function’s docstring.

prompt

Mark a method as an MCP prompt template, listed via prompts/list and rendered via prompts/get using the given (or inferred) name. The method’s signature is introspected the same way as for tools.

resolve_description

Resolve MCP metadata description.

resource

Mark a method as an MCP resource, addressable at uri and readable via the resources/read RPC method.

resource_template

Mark a method as an MCP resource template.

schema_from_signature

Build a JSON Schema “object” definition from a function’s signature, using its type hints for property types and the presence/absence of a default value to determine which parameters are required. self is skipped. Unannotated parameters default to “string”.

tool

Mark a method as an MCP tool. The method’s signature is introspected to build its JSON Schema inputSchema, and it becomes callable via the tools/call RPC method using the given (or inferred) name.

Data

PY_TO_JSON_TYPE

API

duck.contrib.mcp.decorators.PY_TO_JSON_TYPE

None

duck.contrib.mcp.decorators.get_summary_from_docstring(func) str[source]

Extract a short description from a function’s docstring.

The function uses the docstring summary section, stopping before common Google-style docstring sections such as Args, Returns, Raises, Examples, and Notes.

Parameters:

func – Function whose docstring should be inspected.

Returns:

A cleaned description string, or an empty string when no docstring exists.

duck.contrib.mcp.decorators.prompt(description: str = '', name: str = None, scopes: list = None)[source]

Mark a method as an MCP prompt template, listed via prompts/list and rendered via prompts/get using the given (or inferred) name. The method’s signature is introspected the same way as for tools.

scopes, if given, are required scopes checked against self.granted_scopes (set in authenticate()) before it’s rendered.

duck.contrib.mcp.decorators.resolve_description(func, description: str = '') str[source]

Resolve MCP metadata description.

Explicit decorator descriptions take priority. If no description is provided, the function docstring summary is used.

Parameters:
  • func – Function being decorated.

  • description – Explicit MCP description.

Returns:

Resolved description string.

duck.contrib.mcp.decorators.resource(uri: str, name: str, description: str = '', mime_type: str = 'text/plain', scopes: list = None)[source]

Mark a method as an MCP resource, addressable at uri and readable via the resources/read RPC method.

The method should take no arguments (beyond self) and return the resource’s content.

Parameters:
  • uri – Unique URI identifying the resource.

  • name – Human-readable resource name displayed by MCP clients.

  • description – Optional description of the resource.

  • mime_type – MIME type of the returned resource content.

  • scopes – Optional authorization scopes required to read the resource. Checked against self.granted_scopes (set in authenticate()).

Raises:

TypeError – If the decorated method is not asynchronous.

duck.contrib.mcp.decorators.resource_template(uri_template: str, name: str, description: str = '', mime_type: str = 'text/plain', scopes: list = None)[source]

Mark a method as an MCP resource template.

Resource templates describe dynamic resources that can be resolved from URI templates. The method should accept the extracted URI template arguments and return the resource content.

Parameters:
  • uri_template – URI template used to match dynamic resources.

  • name – Human-readable resource template name.

  • description – Optional description of the resource template.

  • mime_type – MIME type of the returned resource content.

  • scopes – Optional authorization scopes required to access the resource.

Raises:

TypeError – If the decorated method is not asynchronous.

duck.contrib.mcp.decorators.schema_from_signature(func) dict[source]

Build a JSON Schema “object” definition from a function’s signature, using its type hints for property types and the presence/absence of a default value to determine which parameters are required. self is skipped. Unannotated parameters default to “string”.

duck.contrib.mcp.decorators.tool(description: str = '', name: str = None, scopes: list = None)[source]

Mark a method as an MCP tool. The method’s signature is introspected to build its JSON Schema inputSchema, and it becomes callable via the tools/call RPC method using the given (or inferred) name.

scopes, if given, are required scopes checked against self.granted_scopes (set in authenticate()) before the tool runs.