=head1 NAME AnyEvent::MCP::Permissions::Helper - writing and integrating permission helpers =head1 SYNOPSIS A permission helper is an executable. It receives a description of an operation on stdin and returns a permission decision on stdout. It does not need to be written in Perl, link against AnyEvent, or implement MCP. For example, a helper that always refuses unresolved permission requests: #!/bin/sh cat >/dev/null || exit 2 exit 1 Select a custom helper in the environment of the server or multibroker: export PERL_ANYEVENT_MCP_PERMISSION_HELPER=/absolute/path/to/my-helper The default helper is L, which asks the user using YAD or a terminal. To use an existing general-purpose MCP form renderer instead, run C as the helper. =head1 INTRODUCTION An MCP server sometimes needs a decision before it can perform an operation. The permission engine first consults its configured and saved rules. If these do not decide the request, it offers the operation to a permission helper. The helper is a delegated decision-maker, not necessarily a dialog program. It can implement a policy, consult another service, display a graphical dialog, ask on a terminal, or combine these approaches. The engine tells it what choices are available. The helper selects one of them. The engine validates the answer, constructs any persistent rule, and determines the sandbox privileges actually granted to the operation. This separation is useful in three different kinds of helpers: =over =item Application-specific helpers Understand a particular server and its operation metadata. For example, a helper for a known file server can recognize a particular operation in a particular working directory. =item Generic permission helpers Understand decisions, sandbox grants, scope choices and lifetimes. They can display or select these without understanding what an individual server's action or dimension means. =item Generic form renderers Understand neither permissions nor application metadata. They display ordinary MCP elicitation forms and return form answers. The bundled permission-to-form adapter handles the rest. =back You can implement any of these without knowing how rule files are written, how the engine constructs matchers, or how MCP continuations work. This document describes the external contract and how to integrate it. For the Perl calling interface, see L. For using permissions and choosing where to ask, see L. For the authorization engine and rule format, see L. =head1 TUTORIAL =head2 Your first helper Save the following as F and make it executable: #!/bin/sh cat >/dev/null || exit 2 exit 1 Configure a server to use it, e.g. (substitute an existing workdir): chmod +x ./deny-helper PERL_ANYEVENT_MCP_PERMISSION_HELPER="$PWD/deny-helper" \ mcp-fs --stdio --ask helper --name project /srv/project Run the server through your usual MCP client. The helper is invoked only when a permission check needs a decision; existing allow and deny rules still apply. The new helper denies those unresolved requests without displaying a dialog. Why does it read stdin if it doesn't care about the request? The caller sends a complete JSON object, then closes its input side of the connection. The helper must consume all input through EOF before exiting. An early exit can cause the caller's write to fail. The helper's stdout is reserved for its answer; send diagnostic messages to stderr. The helper equivalent to an unconditional allow-once is: #!/bin/sh cat >/dev/null Use this only when every otherwise-unresolved permission request in that server's policy is supposed to be approved. It approves the requested sandbox grants as well as the operation, including broad privileges when requested. It does not just suppress an annoying dialog. B before the semantic helper protocol, empty output with exit status zero was an error. It now means approval. Inspect existing custom helpers, wrappers and dismissal paths before upgrading. In particular, do not let a failed check or a closed dialog accidentally return empty output with exit status zero. =head2 Inspect a real request To see what a server asks for, temporarily use this helper in a trusted test setup: #!/bin/sh cat >&2 || exit 2 exit 1 It sends the request to the server's diagnostic output, then refuses it. Depending on the server's logging arrangement, stderr might end up in a log file. Permission requests can contain paths, commands, source code, mail-related information and other private data; do not leave such a diagnostic helper enabled indiscriminately. Here is a complete example request from a small illustrative file server called C. It has two sandbox grants, with C implying C. Unlike real C, it offers only these two grants. It asks to read a file in a known project: { "version": 1, "program": "example-files", "name": "project", "tool": "read-file", "action": "read", "action_description": "Read file contents", "reason": "Inspect the project README", "sandbox_implementation": "bubblewrap", "sandbox_grants": ["workdir-read"], "sandbox_grant_closure": ["workdir-read"], "sandbox_grant_choices": ["workdir-read", "workdir-write"], "sandbox_grant_descriptions": { "workdir-read": "Read files in the selected workdir", "workdir-write": "Modify files in the selected workdir" }, "selected_workdir": "/srv/project", "paths": ["/srv/project/README"], "dimension_order": ["workdir", "subject"], "dimensions": { "workdir": { "label": "Workdir", "value": "/srv/project", "choices": [ {"id": "exact", "label": "This workdir"}, {"id": "any", "label": "All workdirs"} ], "default": "exact", "custom_regex": true }, "subject": { "label": "File", "value": "/srv/project/README", "choices": [ {"id": "exact", "label": "This file"}, {"id": "directory", "label": "Files in this directory"} ], "default": "exact" } }, "decisions": ["allow-once", "deny-once", "allow-always", "deny-always"], "lifetimes": [ {"value": "indefinite", "label": "Indefinite"}, {"value": "900", "label": "15 minutes"}, {"value": "3600", "label": "1 hour"}, {"value": "57600", "label": "16 hours"} ], "lifetime_default": "indefinite" } The actual input is a single JSON line followed by EOF. Pretty-printing here is just for readability. Save this example as F if you want to experiment with the following programs without a running server. A choice's C is what you return to the engine, and its C