Annotation Mapper¶
Feature spec for code-forge implementation planning. Source: extracted from apcore-mcp/docs/tech-design-apcore-mcp.md Created: 2026-04-06
Purpose¶
The Annotation Mapper bridges apcore's behavioral annotations (e.g., destructive, readonly, requires_approval) into the specific metadata formats expected by the Model Context Protocol (MCP) and OpenAI's tool-use API. It provides critical safety hints to AI agents about how to handle tools.
Scope¶
Included:
- Mapping ModuleAnnotations to MCP ToolAnnotations hints (destructiveHint, readOnlyHint, etc.).
- Generation of a parseable description suffix for OpenAI tool descriptions (since OpenAI tools lack native annotation support).
- Identification of high-consequence operations (e.g., those requiring human-in-the-loop approval).
- Inclusion of streaming status in annotations for agent awareness.
Excluded:
- Enforcement of these behavioral rules (the Executor or ApprovalHandler handles enforcement).
- Translation of non-standard, user-defined annotations.
Core Responsibilities¶
- MCP Mapping — Maps apcore's boolean annotation fields to their MCP
ToolAnnotationsequivalents. - OpenAI Description Embedding — Appends structured, machine-readable suffixes to tool descriptions when using the OpenAI adapter.
- Safety Warning Generation — Identifies non-default, high-risk annotations and provides formatted warnings for display in agent interfaces.
Interfaces¶
Inputs¶
- ModuleAnnotations (apcore SDK) — The source metadata from the apcore
ModuleDescriptor.
Outputs¶
- ToolAnnotations (MCP SDK) — The protocol-specific hint object for the tool.
- Annotation Suffix String (OpenAI Converter) — A formatted string added to the tool description.
Dependencies¶
- apcore SDK (language-equivalent: apcore-python / apcore-js / apcore Rust crate) — Provides the
ModuleAnnotationsdataclass. - MCP SDK (language-equivalent: mcp Python / @modelcontextprotocol/sdk / mcp-sdk Rust crate) — Provides the
ToolAnnotationstype definition.
Data Flow¶
graph LR
A[ModuleAnnotations] --> B[Map to MCP Hints]
A --> C[Build Description Suffix]
B --> D[MCP Tool Output]
C --> E[OpenAI Tool Description]
Key Behaviors¶
Description Suffix Generation¶
For OpenAI tool descriptions, a suffix is generated only for non-default values:
1. Safety warnings for destructive or requires_approval are prioritized.
2. A structured block [Annotations: readonly=true, idempotent=true] is appended with non-default fields.
3. Default values (readonly=false, destructive=false, idempotent=false, requires_approval=false, open_world=true) are omitted to save tokens.
Approval Check¶
The mapper identifies modules that require a human-in-the-loop approval step based on the requires_approval flag, which can then be used to trigger MCP elicitation or other confirmation flows.
Decorator Metadata Enrichment¶
Beyond behavioral annotations, the mapper also projects auxiliary ModuleDescriptor metadata sourced from @module decorators / YAML bindings into the MCP Tool output:
descriptor.examples— Rendered into the MCPTool.descriptionas anExamples:section with bullet list. The mapper emits at most three bullets (the first three examples), truncating additional entries to keep descriptions bounded. Each bullet uses the example's short-form summary (or stringified input/output pair) and is appended after the primary description text, separated by a blank line.descriptor.tags— Mirrored onto the MCP tool askeywords(list of strings) and, where applicable, a derivedcategoryhint for client-side grouping. Tags remain unmodified (no case normalization) to preserve registry semantics.descriptor.version— Emitted as_meta.versionon the MCPToolobject (MCP_metareserved namespace), allowing clients to surface per-tool version information independent of the server-level version.descriptor.documentation_url— Emitted as_meta.documentationUrl(camelCase per MCP_metaconventions) for clients that link out to external tool documentation.
All four fields are optional: when absent or None on the descriptor, the mapper omits the corresponding output field entirely rather than emitting empty values. This enrichment is additive and must not conflict with existing description suffix generation used by the OpenAI adapter.
Constraints¶
- Case Consistency: Output labels in suffixes must follow a consistent, parseable naming convention.
- Protocol Defaults: When apcore annotations are
None, the mapper must revert to the safe defaults specified by the protocol (e.g.,readOnlyHint=False,openWorldHint=True).
Error Handling¶
- Missing Annotations: Handles
Noneinput gracefully by returning protocol-standard default objects. - Schema Drift: Ignores unknown annotation fields from legacy versions without crashing.
Notes¶
- This component is vital for safe AI-agent interactions with real-world tools. Without these hints, an agent might inadvertently perform destructive operations without human oversight.
Contract: AnnotationMapper.to_description_suffix¶
Inputs¶
- annotations: ModuleAnnotations | None, optional, validates[duck-typed attributes], reject_with=None (returns empty string on None)
Errors¶
- No exceptions raised; all attribute access uses
getattrwith defaults
Returns¶
- On success: str — formatted annotation suffix starting with
\n\nwhen non-empty; empty string""when annotations is None or all values are defaults - mcp_extras: For each key in
annotations.extrastarting withmcp_whose value is a string: - Strip the
mcp_prefix to get the stripped key - Format as
{stripped_key}: {value}(colon-space separator) - Process in alphabetical order, sorted ordinally by code point on the original key — not by locale collation, which would order differently per host
- Append each as
\n{stripped_key}: {value}directly after the[Annotations: ...]block — a single newline between entries, not a blank line - Non-string values are skipped entirely; they are never stringified into the description
- An empty extras dict produces no additional output
Separator — settled 2026-08-18. This spec previously said the entries were
\n\n-delimited while SRS FR-EXTRAANNOT-001 and tech design §6.2 said\n.\nis now normative: it is what Python (annotations.py:76) and TypeScript (annotations.ts:147) emit, and what two of the three doc layers already specified. Rust still emits\n\n(annotations.rs:144) because it followed this spec's earlier wording — that is now a known divergence to fix in the Rust bridge, not a spec question. Concretely, forextra = {"mcp_a": "1", "mcp_b": "2"}with one non-default flag the correct suffix is"\n\n[Annotations: readonly=true]\na: 1\nb: 2". - On failure: returns""(never raises)
Properties¶
- async: false
- thread_safe: true
- pure: true
- idempotent: true
Contract: AnnotationMapper.to_mcp_annotations¶
Naming note: TypeScript exposes this as
toMcpAnnotationsper language convention; Python/Rust useto_mcp_annotations. Rust exposes it as a static/associated function (AnnotationMapper::to_mcp_annotations(...)).
Inputs¶
- annotations: ModuleAnnotations | None, optional, validates[duck-typed attributes], reject_with=None (returns protocol defaults on None)
Errors¶
- No exceptions raised; None input maps to safe defaults
Returns¶
- On success: a dict / object whose keys are MCP-protocol camelCase identifiers —
readOnlyHint,destructiveHint,idempotentHint,openWorldHint,title. The shape isdict[str, Any]in Python, anMcpAnnotationsDictobject in TypeScript, and aHashMap<&'static str, serde_json::Value>(or equivalent struct serialized to camelCase JSON) in Rust. Defaults whenannotationsis None:readOnlyHint=false,destructiveHint=false,idempotentHint=false,openWorldHint=true,title=null(omitted in TS/Rust serializations when null). - On failure: returns default dict (never raises)
Properties¶
- async: false
- thread_safe: true
- pure: true
- idempotent: true
Contract: AnnotationMapper.has_requires_approval¶
Naming note: TypeScript exposes this as
hasRequiresApprovalper language convention; Python/Rust usehas_requires_approval.
Inputs¶
- annotations: ModuleAnnotations | None, optional, validates[has
requires_approvalattribute], reject_with=None (returns False on None)
Errors¶
- No exceptions raised; None input returns False
Returns¶
- On success: bool — True when
annotations.requires_approvalis truthy, False otherwise (including when annotations is None) - On failure: returns False (never raises)
Properties¶
- async: false
- thread_safe: true
- pure: true
- idempotent: true