|
| 1 | +<!doctype html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | +<meta charset="utf-8"> |
| 5 | +<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1"> |
| 6 | +<meta name="generator" content="pdoc3 0.11.6"> |
| 7 | +<title>adcp.server.translate API documentation</title> |
| 8 | +<meta name="description" content="Error translation and request normalization for proxy and custom-transport servers …"> |
| 9 | +<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/13.0.0/sanitize.min.css" integrity="sha512-y1dtMcuvtTMJc1yPgEqF0ZjQbhnc/bFhyvIyVNb9Zk5mIGtqVaAB1Ttl28su8AvFMOY0EwRbAe+HCLqj6W7/KA==" crossorigin> |
| 10 | +<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/13.0.0/typography.min.css" integrity="sha512-Y1DYSb995BAfxobCkKepB1BqJJTPrOp3zPL74AWFugHHmmdcvO+C48WLrUOlhGMc0QG7AE3f7gmvvcrmX2fDoA==" crossorigin> |
| 11 | +<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css" crossorigin> |
| 12 | +<style>:root{--highlight-color:#fe9}.flex{display:flex !important}body{line-height:1.5em}#content{padding:20px}#sidebar{padding:1.5em;overflow:hidden}#sidebar > *:last-child{margin-bottom:2cm}.http-server-breadcrumbs{font-size:130%;margin:0 0 15px 0}#footer{font-size:.75em;padding:5px 30px;border-top:1px solid #ddd;text-align:right}#footer p{margin:0 0 0 1em;display:inline-block}#footer p:last-child{margin-right:30px}h1,h2,h3,h4,h5{font-weight:300}h1{font-size:2.5em;line-height:1.1em}h2{font-size:1.75em;margin:2em 0 .50em 0}h3{font-size:1.4em;margin:1.6em 0 .7em 0}h4{margin:0;font-size:105%}h1:target,h2:target,h3:target,h4:target,h5:target,h6:target{background:var(--highlight-color);padding:.2em 0}a{color:#058;text-decoration:none;transition:color .2s ease-in-out}a:visited{color:#503}a:hover{color:#b62}.title code{font-weight:bold}h2[id^="header-"]{margin-top:2em}.ident{color:#900;font-weight:bold}pre code{font-size:.8em;line-height:1.4em;padding:1em;display:block}code{background:#f3f3f3;font-family:"DejaVu Sans Mono",monospace;padding:1px 4px;overflow-wrap:break-word}h1 code{background:transparent}pre{border-top:1px solid #ccc;border-bottom:1px solid #ccc;margin:1em 0}#http-server-module-list{display:flex;flex-flow:column}#http-server-module-list div{display:flex}#http-server-module-list dt{min-width:10%}#http-server-module-list p{margin-top:0}.toc ul,#index{list-style-type:none;margin:0;padding:0}#index code{background:transparent}#index h3{border-bottom:1px solid #ddd}#index ul{padding:0}#index h4{margin-top:.6em;font-weight:bold}@media (min-width:200ex){#index .two-column{column-count:2}}@media (min-width:300ex){#index .two-column{column-count:3}}dl{margin-bottom:2em}dl dl:last-child{margin-bottom:4em}dd{margin:0 0 1em 3em}#header-classes + dl > dd{margin-bottom:3em}dd dd{margin-left:2em}dd p{margin:10px 0}.name{background:#eee;font-size:.85em;padding:5px 10px;display:inline-block;min-width:40%}.name:hover{background:#e0e0e0}dt:target .name{background:var(--highlight-color)}.name > span:first-child{white-space:nowrap}.name.class > span:nth-child(2){margin-left:.4em}.inherited{color:#999;border-left:5px solid #eee;padding-left:1em}.inheritance em{font-style:normal;font-weight:bold}.desc h2{font-weight:400;font-size:1.25em}.desc h3{font-size:1em}.desc dt code{background:inherit}.source > summary,.git-link-div{color:#666;text-align:right;font-weight:400;font-size:.8em;text-transform:uppercase}.source summary > *{white-space:nowrap;cursor:pointer}.git-link{color:inherit;margin-left:1em}.source pre{max-height:500px;overflow:auto;margin:0}.source pre code{font-size:12px;overflow:visible;min-width:max-content}.hlist{list-style:none}.hlist li{display:inline}.hlist li:after{content:',\2002'}.hlist li:last-child:after{content:none}.hlist .hlist{display:inline;padding-left:1em}img{max-width:100%}td{padding:0 .5em}.admonition{padding:.1em 1em;margin:1em 0}.admonition-title{font-weight:bold}.admonition.note,.admonition.info,.admonition.important{background:#aef}.admonition.todo,.admonition.versionadded,.admonition.tip,.admonition.hint{background:#dfd}.admonition.warning,.admonition.versionchanged,.admonition.deprecated{background:#fd4}.admonition.error,.admonition.danger,.admonition.caution{background:lightpink}</style> |
| 13 | +<style media="screen and (min-width: 700px)">@media screen and (min-width:700px){#sidebar{width:30%;height:100vh;overflow:auto;position:sticky;top:0}#content{width:70%;max-width:100ch;padding:3em 4em;border-left:1px solid #ddd}pre code{font-size:1em}.name{font-size:1em}main{display:flex;flex-direction:row-reverse;justify-content:flex-end}.toc ul ul,#index ul ul{padding-left:1em}.toc > ul > li{margin-top:.5em}}</style> |
| 14 | +<style media="print">@media print{#sidebar h1{page-break-before:always}.source{display:none}}@media print{*{background:transparent !important;color:#000 !important;box-shadow:none !important;text-shadow:none !important}a[href]:after{content:" (" attr(href) ")";font-size:90%}a[href][title]:after{content:none}abbr[title]:after{content:" (" attr(title) ")"}.ir a:after,a[href^="javascript:"]:after,a[href^="#"]:after{content:""}pre,blockquote{border:1px solid #999;page-break-inside:avoid}thead{display:table-header-group}tr,img{page-break-inside:avoid}img{max-width:100% !important}@page{margin:0.5cm}p,h2,h3{orphans:3;widows:3}h1,h2,h3,h4,h5,h6{page-break-after:avoid}}</style> |
| 15 | +<script defer src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js" integrity="sha512-D9gUyxqja7hBtkWpPWGt9wfbfaMGVt9gnyCvYa+jojwwPHLCzUm5i8rpk7vD7wNee9bA35eYIjobYPaQuKS1MQ==" crossorigin></script> |
| 16 | +<script>window.addEventListener('DOMContentLoaded', () => { |
| 17 | +hljs.configure({languages: ['bash', 'css', 'diff', 'graphql', 'ini', 'javascript', 'json', 'plaintext', 'python', 'python-repl', 'rust', 'shell', 'sql', 'typescript', 'xml', 'yaml']}); |
| 18 | +hljs.highlightAll(); |
| 19 | +/* Collapse source docstrings */ |
| 20 | +setTimeout(() => { |
| 21 | +[...document.querySelectorAll('.hljs.language-python > .hljs-string')] |
| 22 | +.filter(el => el.innerHTML.length > 200 && ['"""', "'''"].includes(el.innerHTML.substring(0, 3))) |
| 23 | +.forEach(el => { |
| 24 | +let d = document.createElement('details'); |
| 25 | +d.classList.add('hljs-string'); |
| 26 | +d.innerHTML = '<summary>"""</summary>' + el.innerHTML.substring(3); |
| 27 | +el.replaceWith(d); |
| 28 | +}); |
| 29 | +}, 100); |
| 30 | +})</script> |
| 31 | +</head> |
| 32 | +<body> |
| 33 | +<main> |
| 34 | +<article id="content"> |
| 35 | +<header> |
| 36 | +<h1 class="title">Module <code>adcp.server.translate</code></h1> |
| 37 | +</header> |
| 38 | +<section id="section-intro"> |
| 39 | +<p>Error translation and request normalization for proxy and custom-transport servers.</p> |
| 40 | +<p>Standard servers using <code>serve()</code> or <code>ADCPAgentExecutor</code> do not need these |
| 41 | +helpers — the framework handles error translation and request normalization |
| 42 | +internally.</p> |
| 43 | +<p>These are for <strong>proxy servers</strong> that catch <code>ADCPError</code> from a downstream |
| 44 | +agent call and need to format it for their own transport, or custom |
| 45 | +multi-transport servers that bypass the standard framework.</p> |
| 46 | +<p>Not exported from <code><a title="adcp.server" href="index.html">adcp.server</a></code> — import directly::</p> |
| 47 | +<pre><code>from adcp.server.translate import translate_error, normalize_request |
| 48 | + |
| 49 | +# In a proxy catching errors from a downstream agent: |
| 50 | +try: |
| 51 | + result = await downstream_client.create_media_buy(params) |
| 52 | +except ADCPError as e: |
| 53 | + raise translate_error(e, protocol="a2a") |
| 54 | + # Raises: ServerError(InternalError(message="...", data={...})) |
| 55 | + |
| 56 | +# Normalize deprecated field names from older callers: |
| 57 | +params = normalize_request(params, task_name="create_media_buy") |
| 58 | +</code></pre> |
| 59 | +</section> |
| 60 | +<section> |
| 61 | +</section> |
| 62 | +<section> |
| 63 | +</section> |
| 64 | +<section> |
| 65 | +<h2 class="section-title" id="header-functions">Functions</h2> |
| 66 | +<dl> |
| 67 | +<dt id="adcp.server.translate.normalize_request"><code class="name flex"> |
| 68 | +<span>def <span class="ident">normalize_request</span></span>(<span>params: dict[str, Any], task_name: str | None = None) ‑> dict[str, typing.Any]</span> |
| 69 | +</code></dt> |
| 70 | +<dd> |
| 71 | +<details class="source"> |
| 72 | +<summary> |
| 73 | +<span>Expand source code</span> |
| 74 | +</summary> |
| 75 | +<pre><code class="python">def normalize_request( |
| 76 | + params: dict[str, Any], |
| 77 | + task_name: str | None = None, |
| 78 | +) -> dict[str, Any]: |
| 79 | + """Normalize deprecated field names and structures in request params. |
| 80 | + |
| 81 | + Applies known transforms so servers can accept both old and new field |
| 82 | + formats without duplicating normalization logic in every handler. |
| 83 | + |
| 84 | + Transforms applied: |
| 85 | + |
| 86 | + - ``account_id: "123"`` → ``account: {account_id: "123"}`` (structural) |
| 87 | + - ``brand_manifest: "https://..."`` → ``brand: {domain: "..."}`` (URL parse) |
| 88 | + - ``promoted_offerings`` → ``catalogs`` (rename) |
| 89 | + - ``campaign_ref`` → ``buyer_campaign_ref`` (create_media_buy only) |
| 90 | + - Package-level ``optimization_goal`` → ``optimization_goals`` (scalar→array) |
| 91 | + - Package-level ``catalog`` → ``catalogs`` (scalar→array) |
| 92 | + |
| 93 | + If both the deprecated and current field name are present, the current |
| 94 | + name takes precedence and the deprecated name is removed. |
| 95 | + |
| 96 | + Args: |
| 97 | + params: Request parameters dict. |
| 98 | + task_name: ADCP task/tool name (e.g. ``"create_media_buy"``). |
| 99 | + Enables tool-scoped renames when provided. |
| 100 | + |
| 101 | + Returns: |
| 102 | + New dict with deprecated field names replaced by current names. |
| 103 | + Original dict is not mutated (top-level copy; packages list is |
| 104 | + copied if package-level transforms apply). |
| 105 | + """ |
| 106 | + result = dict(params) |
| 107 | + |
| 108 | + # Structural transforms |
| 109 | + _normalize_account(result) |
| 110 | + _normalize_brand_manifest(result) |
| 111 | + |
| 112 | + # Package-level transforms (deep copy the packages list) |
| 113 | + if "packages" in result and isinstance(result["packages"], list): |
| 114 | + result["packages"] = [ |
| 115 | + dict(pkg) if isinstance(pkg, dict) else pkg |
| 116 | + for pkg in result["packages"] |
| 117 | + ] |
| 118 | + _normalize_packages(result) |
| 119 | + |
| 120 | + # Global renames |
| 121 | + for old_name, new_name in _GLOBAL_RENAMES.items(): |
| 122 | + if old_name in result: |
| 123 | + if new_name not in result: |
| 124 | + result[new_name] = result.pop(old_name) |
| 125 | + else: |
| 126 | + del result[old_name] |
| 127 | + |
| 128 | + # Tool-scoped renames |
| 129 | + if task_name: |
| 130 | + tool_renames = _TOOL_RENAMES.get(task_name, {}) |
| 131 | + for old_name, new_name in tool_renames.items(): |
| 132 | + if old_name in result: |
| 133 | + if new_name not in result: |
| 134 | + result[new_name] = result.pop(old_name) |
| 135 | + else: |
| 136 | + del result[old_name] |
| 137 | + |
| 138 | + return result</code></pre> |
| 139 | +</details> |
| 140 | +<div class="desc"><p>Normalize deprecated field names and structures in request params.</p> |
| 141 | +<p>Applies known transforms so servers can accept both old and new field |
| 142 | +formats without duplicating normalization logic in every handler.</p> |
| 143 | +<p>Transforms applied:</p> |
| 144 | +<ul> |
| 145 | +<li><code>account_id: "123"</code> → <code>account: {account_id: "123"}</code> (structural)</li> |
| 146 | +<li><code>brand_manifest: "https://..."</code> → <code>brand: {domain: "..."}</code> (URL parse)</li> |
| 147 | +<li><code>promoted_offerings</code> → <code>catalogs</code> (rename)</li> |
| 148 | +<li><code>campaign_ref</code> → <code>buyer_campaign_ref</code> (create_media_buy only)</li> |
| 149 | +<li>Package-level <code>optimization_goal</code> → <code>optimization_goals</code> (scalar→array)</li> |
| 150 | +<li>Package-level <code>catalog</code> → <code>catalogs</code> (scalar→array)</li> |
| 151 | +</ul> |
| 152 | +<p>If both the deprecated and current field name are present, the current |
| 153 | +name takes precedence and the deprecated name is removed.</p> |
| 154 | +<h2 id="args">Args</h2> |
| 155 | +<dl> |
| 156 | +<dt><strong><code>params</code></strong></dt> |
| 157 | +<dd>Request parameters dict.</dd> |
| 158 | +<dt><strong><code>task_name</code></strong></dt> |
| 159 | +<dd>ADCP task/tool name (e.g. <code>"create_media_buy"</code>). |
| 160 | +Enables tool-scoped renames when provided.</dd> |
| 161 | +</dl> |
| 162 | +<h2 id="returns">Returns</h2> |
| 163 | +<p>New dict with deprecated field names replaced by current names. |
| 164 | +Original dict is not mutated (top-level copy; packages list is |
| 165 | +copied if package-level transforms apply).</p></div> |
| 166 | +</dd> |
| 167 | +<dt id="adcp.server.translate.translate_error"><code class="name flex"> |
| 168 | +<span>def <span class="ident">translate_error</span></span>(<span>exc: ADCPError | Error, protocol: "Literal['mcp', 'a2a'] | Protocol") ‑> mcp.server.fastmcp.exceptions.ToolError | a2a.utils.errors.ServerError</span> |
| 169 | +</code></dt> |
| 170 | +<dd> |
| 171 | +<details class="source"> |
| 172 | +<summary> |
| 173 | +<span>Expand source code</span> |
| 174 | +</summary> |
| 175 | +<pre><code class="python">def translate_error( |
| 176 | + exc: ADCPError | Error, |
| 177 | + protocol: Literal["mcp", "a2a"] | Protocol, |
| 178 | +) -> ToolError | ServerError: |
| 179 | + """Translate an AdCP error to a protocol SDK error type. |
| 180 | + |
| 181 | + Returns an error that can be directly raised in a protocol handler:: |
| 182 | + |
| 183 | + try: |
| 184 | + result = await handler.create_media_buy(params) |
| 185 | + except ADCPError as e: |
| 186 | + raise translate_error(e, protocol="mcp") |
| 187 | + |
| 188 | + For MCP, returns ``ToolError`` (from ``mcp.server.fastmcp``). |
| 189 | + For A2A, returns ``ServerError`` wrapping ``InvalidParamsError`` |
| 190 | + (for correctable errors) or ``InternalError`` (for transient/terminal). |
| 191 | + |
| 192 | + The ``data`` field on A2A errors preserves recovery classification, |
| 193 | + error_code, suggestion, and details so buyer agents can make |
| 194 | + retry/fix/abandon decisions. |
| 195 | + |
| 196 | + Args: |
| 197 | + exc: An ADCPError exception or an Error Pydantic model. |
| 198 | + protocol: Target protocol - ``"mcp"`` or ``"a2a"``. |
| 199 | + |
| 200 | + Returns: |
| 201 | + ``ToolError`` for MCP, ``ServerError`` for A2A. Raise the result. |
| 202 | + |
| 203 | + Raises: |
| 204 | + ValueError: If protocol is not ``"mcp"`` or ``"a2a"``. |
| 205 | + |
| 206 | + Warning: |
| 207 | + Error details are passed through to the caller. Do not include |
| 208 | + internal state (stack traces, SQL queries, internal URLs) in |
| 209 | + Error objects passed to this function. |
| 210 | + """ |
| 211 | + proto = protocol.value if isinstance(protocol, Protocol) else str(protocol) |
| 212 | + proto = proto.lower() |
| 213 | + if proto not in ("mcp", "a2a"): |
| 214 | + raise ValueError(f"protocol must be 'mcp' or 'a2a', got {protocol!r}") |
| 215 | + |
| 216 | + # Extract structured fields from the input |
| 217 | + if isinstance(exc, Error): |
| 218 | + code = exc.code |
| 219 | + message = exc.message |
| 220 | + suggestion = exc.suggestion |
| 221 | + details = exc.details |
| 222 | + recovery = _recovery_for_code(code) |
| 223 | + errors = None |
| 224 | + elif isinstance(exc, ADCPError): |
| 225 | + code = _error_code_for_exception(exc) |
| 226 | + message = exc.message |
| 227 | + suggestion = exc.suggestion |
| 228 | + recovery = _recovery_for_code(code) |
| 229 | + details = None |
| 230 | + errors = getattr(exc, "errors", None) |
| 231 | + else: |
| 232 | + raise TypeError(f"Expected ADCPError or Error, got {type(exc).__name__}") |
| 233 | + |
| 234 | + if proto == "mcp": |
| 235 | + return _to_mcp(code, message, suggestion=suggestion) |
| 236 | + return _to_a2a( |
| 237 | + code, message, |
| 238 | + recovery=recovery, suggestion=suggestion, |
| 239 | + details=details, errors=errors, |
| 240 | + )</code></pre> |
| 241 | +</details> |
| 242 | +<div class="desc"><p>Translate an AdCP error to a protocol SDK error type.</p> |
| 243 | +<p>Returns an error that can be directly raised in a protocol handler::</p> |
| 244 | +<pre><code>try: |
| 245 | + result = await handler.create_media_buy(params) |
| 246 | +except ADCPError as e: |
| 247 | + raise translate_error(e, protocol="mcp") |
| 248 | +</code></pre> |
| 249 | +<p>For MCP, returns <code>ToolError</code> (from <code>mcp.server.fastmcp</code>). |
| 250 | +For A2A, returns <code>ServerError</code> wrapping <code>InvalidParamsError</code> |
| 251 | +(for correctable errors) or <code>InternalError</code> (for transient/terminal).</p> |
| 252 | +<p>The <code>data</code> field on A2A errors preserves recovery classification, |
| 253 | +error_code, suggestion, and details so buyer agents can make |
| 254 | +retry/fix/abandon decisions.</p> |
| 255 | +<h2 id="args">Args</h2> |
| 256 | +<dl> |
| 257 | +<dt><strong><code>exc</code></strong></dt> |
| 258 | +<dd>An ADCPError exception or an Error Pydantic model.</dd> |
| 259 | +<dt><strong><code>protocol</code></strong></dt> |
| 260 | +<dd>Target protocol - <code>"mcp"</code> or <code>"a2a"</code>.</dd> |
| 261 | +</dl> |
| 262 | +<h2 id="returns">Returns</h2> |
| 263 | +<p><code>ToolError</code> for MCP, <code>ServerError</code> for A2A. Raise the result.</p> |
| 264 | +<h2 id="raises">Raises</h2> |
| 265 | +<dl> |
| 266 | +<dt><code>ValueError</code></dt> |
| 267 | +<dd>If protocol is not <code>"mcp"</code> or <code>"a2a"</code>.</dd> |
| 268 | +</dl> |
| 269 | +<h2 id="warning">Warning</h2> |
| 270 | +<p>Error details are passed through to the caller. Do not include |
| 271 | +internal state (stack traces, SQL queries, internal URLs) in |
| 272 | +Error objects passed to this function.</p></div> |
| 273 | +</dd> |
| 274 | +</dl> |
| 275 | +</section> |
| 276 | +<section> |
| 277 | +</section> |
| 278 | +</article> |
| 279 | +<nav id="sidebar"> |
| 280 | +<div class="toc"> |
| 281 | +<ul></ul> |
| 282 | +</div> |
| 283 | +<ul id="index"> |
| 284 | +<li><h3>Super-module</h3> |
| 285 | +<ul> |
| 286 | +<li><code><a title="adcp.server" href="index.html">adcp.server</a></code></li> |
| 287 | +</ul> |
| 288 | +</li> |
| 289 | +<li><h3><a href="#header-functions">Functions</a></h3> |
| 290 | +<ul class=""> |
| 291 | +<li><code><a title="adcp.server.translate.normalize_request" href="#adcp.server.translate.normalize_request">normalize_request</a></code></li> |
| 292 | +<li><code><a title="adcp.server.translate.translate_error" href="#adcp.server.translate.translate_error">translate_error</a></code></li> |
| 293 | +</ul> |
| 294 | +</li> |
| 295 | +</ul> |
| 296 | +</nav> |
| 297 | +</main> |
| 298 | +<footer id="footer"> |
| 299 | +<p>Generated by <a href="https://pdoc3.github.io/pdoc" title="pdoc: Python API documentation generator"><cite>pdoc</cite> 0.11.6</a>.</p> |
| 300 | +</footer> |
| 301 | +</body> |
| 302 | +</html> |
0 commit comments