ó
    ‚¤j[ü  ã                   óþ  • S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKrS SK	J
r
Jr  S SKJr  S SKJr  S SKJrJrJrJr  S SKJrJr  S SKJr  S SKrS SKJr  S SKJrJ r   S S	KJ!r!J"r"  S S
K#J$r$  S SK%J&r&  S SK'J(r(  S SK)J*r*  S SK+J,r,J-r-J.r.  S SK/J0r0J1r1  S SK2J3r3J4r4J5r5  S SK6J7r7J8r8Jr  S SK9J:r:  S SK;J<r<  S SK=J>r>  S SK?J@r@  \(       a   S SKAJBrB  S SKCJDrD  S SKEJFrF  S SK9JGrGJHrH  S SK;JIrI  \R”                  R—                  S5      =(       d     \L" \" \Rš                  " 5       5      S-  5      rNSrO " S S5      rP S)S \LS!\LS-  S"\LS-  4S# jjrQS)S$\LS-  S!\LS-  4S% jjrR    S*S$\LS-  S!\LS-  S&\SS'\T\L\4   S-  4S( jjrUg)+é    N)ÚAsyncIteratorÚSequence)ÚBytesIO)ÚPath)ÚTYPE_CHECKINGÚAnyÚOptionalÚcast)ÚunquoteÚurlparse)Úrun_sync)ÚClientÚhandle_file)ÚStatusÚStatusUpdate)ÚImage)ÚAnyUrl)Ú	Starlette)ÚRequest)ÚHTMLResponseÚJSONResponseÚResponse)ÚMountÚRoute)ÚReceiveÚScopeÚSend)Úprocessing_utilsÚroute_utilsÚutils)ÚBlockFunction)ÚState)ÚHeader)ÚSessionState©Útypes©ÚServer©ÚReadResourceContents)ÚBlockContextÚBlocks)Ú	ComponentÚGRADIO_TEMP_DIRÚgradiou—%  <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Gradio MCP Server</title>
<link href="__STATIC_PREFIX__/fonts/SourceSans3/SourceSans3-Regular.woff2" rel="preload" as="font" type="font/woff2" crossorigin />
<link href="__STATIC_PREFIX__/fonts/SourceSans3/SourceSans3-SemiBold.woff2" rel="preload" as="font" type="font/woff2" crossorigin />
<link href="__STATIC_PREFIX__/fonts/IBMPlexMono/IBMPlexMono-Regular.woff2" rel="preload" as="font" type="font/woff2" crossorigin />
<style>
  @font-face {
    font-family: 'Source Sans 3';
    src: url('__STATIC_PREFIX__/fonts/SourceSans3/SourceSans3-Regular.woff2') format('woff2');
    font-weight: 400;
    font-style: normal;
  }
  @font-face {
    font-family: 'Source Sans 3';
    src: url('__STATIC_PREFIX__/fonts/SourceSans3/SourceSans3-SemiBold.woff2') format('woff2');
    font-weight: 600;
    font-style: normal;
  }
  @font-face {
    font-family: 'IBM Plex Mono';
    src: url('__STATIC_PREFIX__/fonts/IBMPlexMono/IBMPlexMono-Regular.woff2') format('woff2');
    font-weight: 400;
    font-style: normal;
  }
  @font-face {
    font-family: 'IBM Plex Mono';
    src: url('__STATIC_PREFIX__/fonts/IBMPlexMono/IBMPlexMono-Medium.woff2') format('woff2');
    font-weight: 500;
    font-style: normal;
  }
  :root {
    --primary: #ff7c00;
    --primary-soft: #fff2e5;
    --text: #1f2937;
    --text-muted: #6b7280;
    --border: #e5e7eb;
    --bg: #f6f7f8;
    --card: #ffffff;
    --radius: 12px;
    --mono: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    --sans: "Source Sans 3", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  }
  * { box-sizing: border-box; }
  body {
    font-family: var(--sans);
    line-height: 1.6;
    color: var(--text);
    background: var(--bg);
    margin: 0;
    padding: 3rem 1.25rem 4rem;
  }
  .wrap { max-width: 46rem; margin: 0 auto; }
  header { margin-bottom: 1.75rem; }
  h1 { font-size: 1.7rem; font-weight: 700; margin: 0 0 0.4rem; letter-spacing: -0.01em; }
  header p { margin: 0; color: var(--text-muted); }
  .card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.4rem 1.5rem;
    margin-bottom: 1.25rem;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
  }
  .card h2 {
    font-size: 0.78rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--primary);
    margin: 0 0 0.85rem;
  }
  .url-box {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: #fafafa;
    padding: 0.7rem 0.85rem;
  }
  .url-box code {
    flex: 1;
    font-family: var(--mono);
    font-size: 0.95rem;
    color: var(--text);
    word-break: break-all;
    background: none;
  }
  .copy-btn {
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.35rem;
    border: 1px solid var(--border);
    background: #fff;
    color: var(--text-muted);
    border-radius: 7px;
    padding: 0.35rem 0.55rem;
    font-family: var(--sans);
    font-size: 0.8rem;
    cursor: pointer;
    transition: all 0.12s ease;
  }
  .copy-btn:hover { border-color: var(--primary); color: var(--primary); }
  .copy-btn.copied { border-color: var(--primary); color: var(--primary); background: var(--primary-soft); }
  .tabs {
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem 1.25rem;
    border-bottom: 1px solid var(--border);
    margin-bottom: 1.1rem;
  }
  .tab {
    appearance: none;
    border: none;
    background: none;
    font-family: var(--sans);
    font-size: 0.95rem;
    color: var(--text-muted);
    padding: 0.5rem 0;
    cursor: pointer;
    border-bottom: 2px solid transparent;
    margin-bottom: -1px;
    transition: color 0.12s ease;
  }
  .tab:hover { color: var(--text); }
  .tab.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }
  .instruction { margin: 0 0 0.7rem; font-size: 0.95rem; }
  .instruction code { font-family: var(--mono); font-size: 0.85em; background: var(--primary-soft); color: #b45309; padding: 0.1rem 0.35rem; border-radius: 5px; }
  .code-block { position: relative; }
  .code-block pre {
    margin: 0;
    background: #0b1221;
    color: #e2e8f0;
    border-radius: 10px;
    padding: 1rem 1.1rem;
    overflow-x: auto;
    font-family: var(--mono);
    font-size: 0.88rem;
    line-height: 1.55;
  }
  .code-block .copy-btn {
    position: absolute;
    top: 0.6rem;
    right: 0.6rem;
    background: rgba(255, 255, 255, 0.08);
    border-color: rgba(255, 255, 255, 0.15);
    color: #cbd5e1;
  }
  .code-block .copy-btn:hover { background: rgba(255, 124, 0, 0.18); border-color: var(--primary); color: #fff; }
  .code-block .copy-btn.copied { background: rgba(255, 124, 0, 0.25); border-color: var(--primary); color: #fff; }
  footer { color: var(--text-muted); font-size: 0.9rem; margin-top: 0.5rem; }
  footer a { color: var(--primary); text-decoration: none; }
  footer a:hover { text-decoration: underline; }
</style>
</head>
<body>
  <div class="wrap">
    <header>
      <h1>ðŸ› ï¸� Gradio MCP Server</h1>
      <p>This is the <strong>Model Context Protocol (MCP)</strong> endpoint for a Gradio app. It is meant to be connected to by an MCP client (such as an AI assistant), not opened directly in a browser.</p>
    </header>

    <section class="card">
      <h2>MCP Server URL</h2>
      <div class="url-box">
        <code id="server-url">__SERVER_URL_TEXT__</code>
        <button class="copy-btn" data-copy-target="server-url" type="button">Copy</button>
      </div>
    </section>

    <section class="card">
      <h2>Connect your AI assistant</h2>
      <div class="tabs" id="tabs"></div>
      <p class="instruction" id="instruction"></p>
      <div class="code-block">
        <button class="copy-btn" data-copy-target="snippet" type="button">Copy</button>
        <pre><code id="snippet"></code></pre>
      </div>
    </section>

    <footer>
      Need help? Read the <a id="docs-link" href="#">Gradio MCP guide</a>.
    </footer>
  </div>

<script>
  const SERVER_URL = __SERVER_URL__;
  const DOCS_URL = __DOCS_URL__;
  document.getElementById("docs-link").href = DOCS_URL;

  const CLIENTS = [
    {
      id: "cursor",
      name: "Cursor",
      instruction: 'Add this to your <code>~/.cursor/mcp.json</code>:',
      config: { mcpServers: { gradio: { url: SERVER_URL } } },
    },
    {
      id: "claude-desktop",
      name: "Claude Desktop",
      instruction: 'Add this to your <code>claude_desktop_config.json</code> (requires Node.js):',
      config: { mcpServers: { gradio: { command: "npx", args: ["mcp-remote", SERVER_URL] } } },
    },
    {
      id: "claude-code",
      name: "Claude Code",
      instruction: "Run this command in your terminal:",
      command: "claude mcp add --transport http gradio " + SERVER_URL,
    },
    {
      id: "vscode",
      name: "VS Code",
      instruction: 'Add this to <code>.vscode/mcp.json</code> in your workspace:',
      config: { servers: { gradio: { type: "http", url: SERVER_URL } } },
    },
    {
      id: "cline",
      name: "Cline",
      instruction: 'Add this to your <code>cline_mcp_settings.json</code>:',
      config: { mcpServers: { gradio: { type: "streamableHttp", url: SERVER_URL } } },
    },
    {
      id: "windsurf",
      name: "Windsurf",
      instruction: 'Add this to your <code>~/.codeium/windsurf/mcp_config.json</code>:',
      config: { mcpServers: { gradio: { serverUrl: SERVER_URL } } },
    },
  ];

  const tabsEl = document.getElementById("tabs");
  const instructionEl = document.getElementById("instruction");
  const snippetEl = document.getElementById("snippet");
  let active = CLIENTS[0].id;

  function snippetFor(client) {
    return client.command ? client.command : JSON.stringify(client.config, null, 2);
  }

  function render() {
    tabsEl.innerHTML = "";
    CLIENTS.forEach(function (client) {
      const btn = document.createElement("button");
      btn.type = "button";
      btn.className = "tab" + (client.id === active ? " active" : "");
      btn.textContent = client.name;
      btn.addEventListener("click", function () {
        active = client.id;
        render();
      });
      tabsEl.appendChild(btn);
    });
    const client = CLIENTS.find(function (c) { return c.id === active; });
    instructionEl.innerHTML = client.instruction;
    snippetEl.textContent = snippetFor(client);
  }

  function copyText(text, btn) {
    const done = function () {
      const original = btn.textContent;
      btn.textContent = "Copied!";
      btn.classList.add("copied");
      setTimeout(function () {
        btn.textContent = original;
        btn.classList.remove("copied");
      }, 1500);
    };
    if (navigator.clipboard && navigator.clipboard.writeText) {
      navigator.clipboard.writeText(text).then(done, function () {});
    } else {
      const ta = document.createElement("textarea");
      ta.value = text;
      document.body.appendChild(ta);
      ta.select();
      try { document.execCommand("copy"); done(); } catch (e) {}
      document.body.removeChild(ta);
    }
  }

  document.addEventListener("click", function (event) {
    const btn = event.target.closest("[data-copy-target]");
    if (!btn) return;
    const target = document.getElementById(btn.getAttribute("data-copy-target"));
    if (target) copyText(target.textContent, btn);
  });

  render();
</script>
</body>
</html>
c                   ó&  • \ rS rSrSr SSKJr  SSKJr  SSK	J
r
  SSKJr  SSKJr  SHS	 jr\S
\S-  4S j5       r\S\S
\4S j5       r\S\S
\4S j5       rS\S
\4S jrS
\\   S-  4S jr\S\S\\   S
\4S j5       rS
\\\4   4S jrSIS jr S
\!4S jr"S\S\\\#4   S
\$\\\#   \\\4   S4   4S jr%S\#S
\\#   4S jr&\S \'S
\S-  4S! j5       r(S\#S"\\)-  S
\\\#4   4S# jr*SJS$ jr+S%\,S&\S'\S
S4S( jr-S)\S
S*4S+ jr.\S
\4S, j5       r/S-SS\S
\$\\\\4   4   4S. jr0\S/\1S0   S1\S
\4S2 j5       r2\S3\1S0   S1\#S
\4S4 j5       r3 SKS\S5\\\4   S-  S
\$\\\#4   \\\\)-        4   4S6 jjr4S
\54S7 jr6S8\\\#4   S
\$\\\#4   \\\\)-        4   4S9 jr7S:\#S;\\\\)-        S
\#4S< jr8\S=\S
\9Rr                  S-  4S> j5       r:\S?\#S
\;S-  4S@ j5       r<\SA\9Rr                  SB\S
\4SC j5       r=S1\#SD\S
\SE   4SF jr>SGr?g! \ a     GNçf = f)LÚGradioMCPServeri_  z¤
A class for creating an MCP server around a Gradio app. This class
requires `mcp` to be installed.

Args:
    blocks: The Blocks app to create the MCP server for.
r   r%   r'   r)   )ÚSseServerTransport)ÚStreamableHTTPSessionManagerc                 óš  ^ ^•  SS K nUT l        T R                  R                  5       T l        T R                  5       T l        ST l        [        R                  " 5       nU(       a  UR                  S5      S   S-   OST l        T R                  5       T l        T R                  5         S T l        S T l        T R#                  T R                  SSS	9mS
[$        S[&        S[(        SS 4UU 4S jjn[*        R,                  S[.        S[0        S    4U4S jj5       nUT l        TT l        UT l        g ! [         a  n[        S5      UeS nAff = f)Nr   zƒThe `mcp` package is required to use the Gradio MCP integration. Please install it with the `mcp` extra: `pip install gradio[mcp]`.Ú Ú/éÿÿÿÿÚ_FT)ÚappÚjson_responseÚ	statelessÚscopeÚreceiveÚsendÚreturnc              “   óô  >#   • U R                  SS5      nUR                  S5      (       d   [        SU S3SS9nU" XU5      I S h  v•N   g U R                  S5      S	:X  av  S
nU R                  S/ 5       H  u  pgUR                  5       S:X  d  M  Un  O   SU;  a;  T
R	                  U 5      n[        T
R                  U5      S9nU" XU5      I S h  v•N   g T	R                  XU5      I S h  v•N   g  N« N" N	7f)NÚpathr5   )ú/gradio_api/mcpz/gradio_api/mcp/z/gradio_api/mcp/httpz/gradio_api/mcp/http/zPath 'zD' not found. The MCP HTTP transport is available at /gradio_api/mcp.i”  )ÚcontentÚstatus_codeÚmethodÚGETó    Úheaderss   accepts   text/event-stream)rC   )ÚgetÚendswithr   ÚlowerÚ_server_url_from_scoper   Ú_landing_page_htmlÚhandle_request)r<   r=   r>   rA   ÚresponseÚacceptÚkeyÚvalueÚ
server_urlÚmanagerÚselfs            €€Ú˜C:\Users\johnn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\gradio/mcp.pyÚhandle_streamable_httpÚ8GradioMCPServer.__init__.<locals>.handle_streamable_http‹  s  øé € ð —9‘9˜V RÓ(ˆDØ—=‘=ð÷ñ ô $Ø$ T FÐ*nÐoØ #ñ�ñ ˜u¨tÓ4×4Ð4Øð �y‰y˜Ó" eÓ+Ø�Ø"'§)¡)¨I°rÖ":‘J�CØ—y‘y“{ iÕ/Ø!&˜Ùñ #;ð (¨vÓ5Ø!%×!<Ñ!<¸UÓ!C�JÜ+°D×4KÑ4KÈJÓ4WÑX�HÙ" 5°4Ó8×8Ð8Øà×(Ñ(¨¸Ó>×>Ñ>ñ) 5ñ" 9ñ ?ùs>   ƒAC8ÁC2ÁAC8ÂAC8ÃC4ÃC8Ã,C6Ã-C8Ã4C8Ã6C8r9   c                ó´   >#   • TR                  5        ISh  v•N    S7v •   SSS5      ISh  v•N   g N! f = f N! , ISh  v•N  (       d  f       g= f7f)z7Context manager for managing session manager lifecycle.N)Úrun)r9   rT   s    €rV   ÚlifespanÚ*GradioMCPServer.__init__.<locals>.lifespan±  s9   øé € ð —{‘{—}•}ðÜà÷	 %—}’}øð ú÷	 %—}—}�}üsL   ƒA˜5™Aœ>ž7£>¤A¯<°A·9¹>¼A¾AÁAÁAÁA)ÚmcpÚImportErrorÚblocksÚget_api_infoÚapi_infoÚcreate_mcp_serverÚ
mcp_serverÚ	root_pathr    Ú	get_spaceÚsplitÚtool_prefixÚget_tool_to_endpointÚtool_to_endpointÚwarn_about_state_inputsÚ
_local_urlÚ_client_instancer3   r   r   r   Ú
contextlibÚasynccontextmanagerr   r   r[   rT   rW   )rU   r_   r]   ÚeÚspace_idrW   r[   rT   s   `      @rV   Ú__init__ÚGradioMCPServer.__init__t  sO  ù€ ð	Ûð ˆŒØŸ™×0Ñ0Ó2ˆŒØ×0Ñ0Ó2ˆŒØˆŒÜ—?’?Ó$ˆÞ<D˜8Ÿ>™>¨#Ó.¨rÑ2°SÒ8È"ˆÔØ $× 9Ñ 9Ó ;ˆÔØ×$Ñ$Ô&Ø&*ˆŒØ/3ˆÔà×3Ñ3Ø—‘¨uÀð 4ð 
ˆð$	?Üð$	?Ü#*ð$	?Ü26ð$	?à÷$	?ð $	?ôL 
×	'Ñ	'ð	¤	ð 	¬m¸DÑ.Aö 	ó 
(ð	ð !ˆŒØˆŒØ&<ˆÕ#øôK ó 	Üð Vóàðûð	ús   „D/ Ä/
E
Ä9EÅE
r?   Nc                 ó   • U R                   $ ©N)rk   ©rU   s    rV   Ú	local_urlÚGradioMCPServer.local_url¾  s   € à�‰ÐrG   r<   c                 óä  • U R                  S/ 5      =(       d    /  VVs0 s H  u  pUR                  5       U_M     nnnU R                  SS5      nUR                  S5      =nb1  UR                  S5      R                  S5      S   R	                  5       nS	nUR                  S
5      =nb#  UR                  S5      S   R	                  5       nOUR                  S5      =nb  UnUR                  S5      =(       d    Sn	U R                  SS5      n
SU
R	                  S5      -   n
U
R                  S5      (       a  U
SU
R                  S5       n
U
R                  S5      S-   n
U SU	 U
 3$ s  snnf )a  Reconstruct the public URL of the MCP endpoint from an ASGI scope.

Honours ``X-Forwarded-Proto``/``X-Forwarded-Host`` so that the URL
shown on the landing page matches what the user typed into the browser
even when Gradio is behind a proxy (e.g. a Hugging Face Space).
rH   ÚschemeÚhttps   x-forwarded-protoNzlatin-1Ú,r   rG   s   x-forwarded-hostó   ,s   hostÚ	localhostrA   rB   r6   )ú/httpz/http/r~   z://)rI   rK   Údecoderf   ÚstriprJ   ÚrfindÚrstrip)r<   rQ   rR   rH   ry   Úforwarded_protoÚhostÚforwarded_hostÚhost_headerÚhost_strrA   s              rV   rL   Ú&GradioMCPServer._server_url_from_scopeÂ  sf  € ð 9>¿	¹	À)ÈRÓ8P×8VÐTVÐ8VÔWÒ8V©*¨#�3—9‘9“; Ò%Ñ8VˆÑWà—‘˜8 VÓ,ˆØ&Ÿ{™{Ð+?Ó@Ð@ˆOÑMØ$×+Ñ+¨IÓ6×<Ñ<¸SÓAÀ!ÑD×JÑJÓLˆFàˆØ%Ÿk™kÐ*=Ó>Ð>ˆNÑKØ!×'Ñ'¨Ó-¨aÑ0×6Ñ6Ó8‰DØ$Ÿ[™[¨Ó1Ð1ˆkÑ>ØˆDØ—;‘;˜yÓ)×8¨[ˆð �y‰y˜Ð!2Ó3ˆØ�T—Z‘Z “_Ñ$ˆØ�=‰=Ð,×-Ñ-ØÐ-˜$Ÿ*™* WÓ-Ð.ˆDØ�{‰{˜3Ó #Ñ%ˆà�˜˜X˜J t fÐ-Ð-ùó+ Xs   ŸE,rS   c                 ó¢  • Sn[         R                  " U 5      n[         R                  " U5      n[        U 5      nUR                  nSU;   a  UR	                  S5      S   nOUR                  S5      nU S3n[        R                  SU5      R                  SU5      R                  S[        R                  " U 5      5      R                  S	U5      $ )
a›  HTML landing page shown when a browser navigates to the MCP endpoint.

MCP clients connect using the streamable HTTP transport (which requires
an ``Accept: text/event-stream`` header). A plain browser ``GET`` would
otherwise receive a raw JSON-RPC "Not Acceptable" error, so we serve
this informational page instead. It surfaces the server URL and ready
to paste configuration snippets for popular MCP clients.
z=https://www.gradio.app/guides/building-mcp-server-with-gradiorB   r   r6   z/staticÚ__SERVER_URL__Ú__DOCS_URL__Ú__SERVER_URL_TEXT__Ú__STATIC_PREFIX__)
ÚjsonÚdumpsr   rA   rf   r‚   Ú_MCP_LANDING_PAGE_TEMPLATEÚreplaceÚhtmlÚescape)rS   Údocs_urlÚserver_url_jsÚdocs_url_jsÚparsedrA   rd   Ústatic_prefixs           rV   rM   Ú"GradioMCPServer._landing_page_htmlá  s´   € ð SˆÜŸ
š
 :Ó.ˆÜ—j’j Ó*ˆÜ˜*Ó%ˆØ�{‰{ˆØ Ó$ØŸ
™
Ð#4Ó5°aÑ8‰IàŸ™ CÓ(ˆIØ$˜+ WÐ-ˆä&×.Ñ.Ð/?ÀÓOß‰W�^ [Ó1ß‰WÐ*¬D¯KªK¸
Ó,CÓDß‰WÐ(¨-Ó8ð		
rG   Úrequestc                 óÖ   • [         R                  " [        UR                  5      5      nUR	                  SS9n[        U5      R                  S5      nUR                  S5      (       a  gg)z´
Gets the route path of the MCP server based on the incoming request.
Can be different depending on whether the request is coming from the MCP SSE transport or the HTTP transport.
N)Úqueryr6   z/gradio_api/mcp/messagesrB   )ÚhttpxÚURLÚstrÚurlÚ	copy_withr‚   rJ   )rU   rš   r    s      rV   Úget_route_pathÚGradioMCPServer.get_route_pathü  sS   € ô
 �iŠiœ˜GŸK™KÓ(Ó)ˆØ�m‰m $ˆmÐ'ˆÜ�#‹h�o‰o˜cÓ"ˆØ�<‰<Ð2×3Ñ3Ø-à$rG   c                 óö   • U R                   R                  R                  nUc  g[        [	        US0 5      5      nSU;   a4  US   R                  S5      nU Vs/ s H  o@R                  U-   PM     nnU$ gs  snf )zÈ
Extract the selected tools from the request query parameters and return the full tool names (with the tool prefix).
Returns None if no tools parameter is specified (meaning all tools are available).
NÚquery_paramsÚtoolsr{   )rc   Úrequest_contextrš   ÚdictÚgetattrrf   rg   )rU   Úcontext_requestr¥   r¦   ÚtoolÚfull_tool_namess         rV   Úget_selected_tools_from_requestÚ/GradioMCPServer.get_selected_tools_from_request	  s~   € ð
 +/¯/©/×*IÑ*I×*QÑ*QˆØÑ"ØÜœG O°^ÀRÓHÓIˆØ�lÓ"Ø  Ñ)×/Ñ/°Ó4ˆEÙCHÓIÂ5¸4×/Ñ/°$Ô6Á5ˆOÐIØ"Ð"Øùò Js   ÁA6Ú	tool_nameÚexisting_tool_namesc                 óz   • [         R                  " SSU 5      n U SS n U nSnX;   a  USU 3-   n US-  nX;   a  M  U $ )z¤
Sanitizes a tool name to make it a valid MCP tool name (only
alphanumeric characters, underscores, <= 128 characters)
and is unique among the existing tool names.
z[^a-zA-Z0-9]r8   Néx   é   )ÚreÚsub)r¯   r°   Útool_name_baseÚsuffixs       rV   Úvalid_and_unique_tool_nameÚ*GradioMCPServer.valid_and_unique_tool_name  sZ   € ô —F’F˜?¨C°Ó;ˆ	Ø˜d˜s�Oˆ	Ø"ˆØˆØÓ.Ø&¨1¨V¨H¨Ñ5ˆIØ�a‰KˆFð Õ.ð ÐrG   c                 óÜ  • 0 nU R                   S    HÖ  nU R                  U5      nUb  UR                  c  M&  [        UR                  SS5      =(       dV    [	        UR                  S5      =(       a!    [        UR                  R
                  SS5      =(       d    UR                  S5      nU R                  U-   nU R                  U[        UR                  5       5      5      nX!U'   MØ     U$ )z�
Gets all of the tools that are exposed by the Gradio app and also
creates a mapping from the tool names to the endpoint names in the API docs.
Únamed_endpointsNÚ__name__Ú	__class__r6   )ra   Úget_block_fn_from_endpoint_nameÚfnr©   Úhasattrr½   Úlstriprg   r¸   ÚsetÚkeys)rU   ri   Úendpoint_nameÚblock_fnÚfn_namer¯   s         rV   rh   Ú$GradioMCPServer.get_tool_to_endpoint*  sá   € ð
 ÐØ!Ÿ]™]Ð+<Ô=ˆMØ×;Ñ;¸MÓJˆHØÑ 8§;¡;Ñ#6Ùä˜Ÿ™ Z°Ó6÷ -ä˜HŸK™K¨Ó5÷ IÜ §¡× 5Ñ 5°zÀ4ÓH÷-ð
 !×'Ñ'¨Ó,ð ð ×(Ñ(¨7Ñ2ˆIØ×7Ñ7Øœ3Ð/×4Ñ4Ó6Ó7óˆIð +8˜YÓ'ñ! >ð"  ÐrG   c                 óð   • U R                   R                  5        HX  u  pU R                  U5      nU(       d  M  [        S UR                   5       5      (       d  MB  [
        R                  " S5        MZ     g)z-
Warn about tools that have gr.State inputs.
c              3   óB   #   • U  H  n[        U[        5      v •  M     g 7frt   )Ú
isinstancer"   )Ú.0Úinputs     rV   Ú	<genexpr>Ú:GradioMCPServer.warn_about_state_inputs.<locals>.<genexpr>I  s   é € ÐVÂo¸U¤
¨5´%× 8Ð 8Âoùs   ‚z©This MCP server includes a tool that has a gr.State input, which will not be updated between tool calls. The original, default value of the State will be used each time.N)ri   Úitemsr¾   ÚanyÚinputsÚwarningsÚwarn)rU   r8   rÄ   rÅ   s       rV   rj   Ú'GradioMCPServer.warn_about_state_inputsC  sZ   € ð !%× 5Ñ 5× ;Ñ ;Ö =ÑˆAØ×;Ñ;¸MÓJˆHßˆxœCÑVÀhÇoÂoÓV×VÓVÜ—’ð&öò !>rG   c           
      óD  • U R                   cˆ  U R                  R                  R                  nUc  [	        S5      eU R                  U5      n[        R                  " UUU R                  S9n[        U R                  =(       d    USSSSSSS0S9U l         U R                   $ )Nú™Could not find the request object in the MCP server context. This is not expected to happen. Please raise an issue: https://github.com/gradio-app/gradio.©rš   Ú
route_pathrd   Fúx-gradio-userr]   )Údownload_filesÚverboseÚanalytics_enabledÚ
ssl_verifyÚ_skip_componentsrH   )rl   rc   r§   rš   Ú
ValueErrorr¢   r   Úget_root_urlrd   r   rv   )rU   rª   rØ   Úroot_urls       rV   Ú_get_or_create_clientÚ%GradioMCPServer._get_or_create_clientP  s«   € Ø× Ñ Ñ(Ø.2¯o©o×.MÑ.M×.UÑ.UˆOØÑ&Ü ð póð ð ×,Ñ,¨_Ó=ˆJÜ"×/Ò/Ø'Ø%ØŸ.™.ñˆHô
 %+Ø—‘×* (Ø$ØØ"'Ø Ø!&Ø(¨%Ð0ñ%ˆDÔ!ð ×$Ñ$Ð$rG   ÚnameÚ	argumentsr!   c                 ó’  • U R                  5       nU R                  U5      u  pEU R                  X%5      nU R                  R	                  U5      nUc  [        SU 35      eUb  X;  a  [        SU S35      eU R                  U5      nUc   eXpR                  S   ;   a.  U R                  S   U   S   n	[        R                  " U	SU5      n
O/ n
U R                  R                  R                  nUc  [        S5      e[        UR                  R                  5       5      nUR!                  S	S5        UR!                  S
S5        XzXÈ4$ )z…
Prepare and validate arguments for a tool call.

Returns:
    A tuple of (endpoint_name, processed_args, request_headers, block_fn)
Nú"Unknown tool for this Gradio app: zTool 'z#' is not in the selected tools listr»   Ú
parameters© rÖ   zcontent-lengthrÙ   )r­   Úget_input_schemaÚconvert_strings_to_filedatari   rI   rß   r¾   ra   Úclient_utilsÚconstruct_argsrc   r§   rš   r¨   rH   rÏ   Úpop)rU   rä   rå   Úselected_toolsr8   Úfiledata_positionsÚprocessed_kwargsrÄ   rÅ   Úparameters_infoÚprocessed_argsrª   Úrequest_headerss                rV   Ú_prepare_tool_call_argsÚ'GradioMCPServer._prepare_tool_call_argsh  si  € ð ×=Ñ=Ó?ˆØ $× 5Ñ 5°dÓ ;ÑˆØ×;Ñ;Øó
Ðð ×-Ñ-×1Ñ1°$Ó7ˆØÑ ÜÐAÀ$ÀÐHÓIÐIàÑ%¨$Ó*DÜ˜v d VÐ+NÐOÓPÐPà×7Ñ7¸ÓFˆØÑ#Ð#Ð#àŸM™MÐ*;Ñ<Ó<Ø"Ÿm™mÐ,=Ñ>¸}ÑMØñˆOô *×8Ò8ØØØ ó‰Nð  ˆNà*.¯/©/×*IÑ*I×*QÑ*QˆØÑ"Üð lóð ô ˜×6Ñ6×<Ñ<Ó>Ó?ˆØ×ÑÐ,¨dÔ3Ø×Ñ˜O¨TÔ2à¨oÐGÐGrG   Újobc              ƒ   óN   #   • [        UR                  5      I Sh  v•N nU/$  N7f)zÁ
Execute a tool call without progress tracking (fast path).

Calls job.result() to get the final output without processing
intermediate status updates.

Returns:
    The output data as a list.
N)r   Úresult)rU   r÷   rù   s      rV   Ú_execute_tool_without_progressÚ.GradioMCPServer._execute_tool_without_progress—  s$   é € ô   §
¡
Ó+×+ˆØˆxˆñ ,ùs   ‚%›#œ%Úupdatec                 ó  • U R                   [        R                  [        R                  4;   a  gU R                   [        R                  4;   a?  SU R
                   SU R                   S3nU R                  b  USU R                   S3-  nU$ U R                   [        R                  4;   a¤  U R                  =(       d    /  HŠ  nUR                  c  SOS	UR                   3nUR                  b-  UR                  b   U S
UR                   SUR                   3s  $ UR                  c  Mj  UR                  b  My  U S
UR                   3s  $    gU R                   [        R                  [        R                  4;   a  gg)z‘
Format a status update into a human-readable progress message.

Returns:
    A formatted message string, or None if no message should be shown.
zJoined server queue.zIn queue. Position z out of Ú.Nz Estimated time remaining: z	 seconds.ÚProgressz	Progress z: Step z of Ú
Processing)Úcoder   ÚJOINING_QUEUEÚSTARTINGÚIN_QUEUEÚrankÚ
queue_sizeÚetaÚPROGRESSÚprogress_dataÚdescÚindexÚlengthÚ
PROCESSINGÚ	ITERATING)rü   ÚmessageÚprogress_unitÚtitles       rV   Ú_format_progress_messageÚ(GradioMCPServer._format_progress_message¤  sk  € ð �;‰;œ6×/Ñ/´·±ÐAÓAØ)Ø�[‰[œVŸ_™_Ð-Ó-Ø+¨F¯K©K¨=¸À×ARÑARÐ@SÐSTÐUˆGØ�z‰zÑ%ØÐ8¸¿¹¸ÀIÐNÑN�ØˆNØ�[‰[œVŸ_™_Ð-Ó-Ø!'×!5Ñ!5×!;¸Ò!;�ð %×)Ñ)Ñ1ñ à$ ]×%7Ñ%7Ð$8Ð9ð ð
 !×&Ñ&Ñ2°}×7KÑ7KÑ7Wà ˜' ¨×)<Ñ)<Ð(=¸TÀ-×BVÑBVÐAWÐXòð #×(Ñ(Ó4¸×9MÑ9MÓ9UØ#˜W G¨M×,?Ñ,?Ð+@ÐAÒAñ "<ð ð �[‰[œV×.Ñ.´×0@Ñ0@ÐAÓAØØrG   Úprogress_tokenc           	   ƒ   óì  #   • SnU  Sh  v•N nUR                   S:X  a‡  [        [        U5      nU R                  U5      nU R                  R
                  R                  R                  UUU[        U R                  R
                  R                  5      S9I Sh  v•N   US-  nM   UR                   S:X  d  M²  UR                  (       d  MÅ  UR                  nUR                  (       a  Mä  UR                  S5      nUR                  S5      nU(       a  U(       a  U S	U 3n	OU(       a  Un	OU(       a  Un	OS
n	[        U	5      e GN9 N«
 UR                  5       (       a  UR                  5       eWS   $ 7f)z³
Execute a tool call with progress tracking (streaming path).

Iterates through job updates to send progress notifications to the client.

Returns:
    The output data as a list.
r   NÚstatus)r  Úprogressr  Úrelated_request_idr³   Úoutputr  Úerrorz: zError!Údata)Útyper
   r   r  rc   r§   ÚsessionÚsend_progress_notificationrŸ   Ú
request_idÚfinalÚoutputsÚsuccessrI   ÚRuntimeErrorÚ	exception)
rU   r÷   r  Ústeprü   r  r  Úerror_titleÚerror_messageÚmsgs
             rV   Ú_execute_tool_with_progressÚ+GradioMCPServer._execute_tool_with_progressÄ  sF  é € ð ˆÙ÷ 	,�&Ø�{‰{˜hÓ&Üœl¨FÓ3�Ø×7Ñ7¸Ó?�ð —O‘O×3Ñ3×;Ñ;×VÑVØ'5Ø!%Ø 'Ü+.Ø ŸO™O×;Ñ;×FÑFó,ð	 Wð ÷	ð 	ð ˜‘	’Ø—‘ Õ(¨V¯\¯\©\ØŸ™�Ø—~—~‘~Ø"(§*¡*¨WÓ"5�KØ$*§J¡J¨wÓ$7�MÞ"¦}Ø!, ¨R°¨Ð?™Þ&Ø+™Þ$Ø)™à&˜Ü& sÓ+Ð+ò;	,ñ
	ð  ð< �=‰=�?‰?Ø—-‘-“/Ð!Ø�f‰~ÐùsK   ‚E4‡E	‹EŒE	�BE4ÂEÂE4Â9E4ÃE4Ã+AE4ÅE	ÅE4Å	+E4c                 óà  ^ • T R                  [        T R                  R                  =(       d    S5      5      nUR	                  5       S[        S[
        [        [        4   ST R                  R                  4U 4S jj5       nUR                  5       S[        T R                  R                     4U 4S jj5       nUR                  5       S[        T R                  R                     4U 4S jj5       nUR                  5       S[        T R                  R                     4U 4S jj5       nUR!                  5       S	["        [        -  S[        T R$                     4U 4S
 jj5       nUR'                  5       S[        T R                  R(                     4U 4S jj5       nUR+                  5        SS[        S[
        [        [        4   S-  ST R                  R,                  4U 4S jjj5       nU$ )z›
Create an MCP server for the given Gradio Blocks app.

Parameters:
    blocks: The Blocks app to create the MCP server for.

Returns:
    The MCP server.
z
Gradio Apprä   rå   r?   c           	   “   óf  >#   • TR                  X5      u  p#pETR                  UR                  U5      nUR                  (       d]  [	        TR
                  5      nTR
                  R                  UUUTR                  R                  R                  S9I Sh  v•N nUS   nO´Sn	TR                  R                  R                  b*  TR                  R                  R                  R                  n	[        TR                  5      I Sh  v•N n
U
R                  " UUUS.6nU	c  TR                  U5      I Sh  v•N nOTR!                  UU	5      I Sh  v•N nTR#                  UR$                  U5      nTR                  R                  R                  nTR'                  U5      n[(        R*                  " UUTR,                  S9nTR/                  XŽ5      n[1        UR2                  SS5      (       a  SU0nOSnTR4                  R7                  UU[1        UR2                  S	S5      S
9$  GN� GN# Nö NÝ7f)z�
Call a tool on the Gradio app.

Args:
    name: The name of the tool to call.
    arguments: The arguments to pass to the tool.
)rÅ   rÑ   Ústaterš   Nr  )Úapi_namerH   r×   Ú_mcp_structured_outputFrù   Ú	_mcp_meta)rC   ÚstructuredContentÚ_meta)rõ   Úinsert_empty_staterÑ   Úqueuer$   r_   Úprocess_apirc   r§   rš   ÚmetaÚprogressTokenr   râ   Úsubmitrú   r)  Úpop_returned_stater!  r¢   r   rà   rd   Úpostprocess_output_datar©   r¿   r&   ÚCallToolResult)rä   rå   rÄ   ró   rô   rÅ   Úsession_stateÚ
raw_outputÚoutput_datar  Úclientr÷   rª   rØ   rá   rC   Ústructured_contentrU   s                    €rV   Ú	call_toolÚ4GradioMCPServer.create_mcp_server.<locals>.call_toolþ  s	  øé € ð ×,Ñ,¨TÓ=ñ EˆM¨?ð "×4Ñ4°X·_±_ÀnÓUˆNà—>—>ô
 !-¨T¯[©[Ó 9�Ø#'§;¡;×#:Ñ#:Ø%Ø)Ø'Ø ŸO™O×;Ñ;×CÑCð	 $;ð $÷ �
ð )¨Ñ0‘ð "&�Ø—?‘?×2Ñ2×7Ñ7ÑCØ%)§_¡_×%DÑ%D×%IÑ%I×%WÑ%W�Nä'¨×(BÑ(BÓC×C�Ø—m’mØ#Ø*Ø+ò�ð "Ñ)Ø(,×(KÑ(KÈCÓ(P×"P‘Kà(,×(HÑ(HØØ&ó)÷ #�Kð
 ×1Ñ1°(×2BÑ2BÀKÓPˆKà.2¯o©o×.MÑ.M×.UÑ.UˆOØ×,Ñ,¨_Ó=ˆJÜ"×/Ò/Ø'Ø%ØŸ.™.ñˆHð
 ×2Ñ2°;ÓIˆGÜ�x—{‘{Ð$<¸e×DÑDØ&.°Ð%8Ñ"à%)Ð"Ø—:‘:×,Ñ,ØØ"4Ü˜hŸk™k¨;¸Ó=ð -ð ð òWò Dñ #Qñ#ùsJ   ƒBH1ÂH'ÂA/H1ÄH*Ä.H1Ä6H-Ä7H1ÅH/ÅCH1È*H1È-H1È/H1c            
   “   ó  >#   • T
R                  5       n / nT
R                  R                  5        HÏ  u  p#U b  X ;  a  M  T
R                  U5      nUbB  UR                  b5  [        UR                  S5      (       a  UR                  R                  S:w  a  Mg  T
R                  XB5      u  pVT
R                  X&5      u  px[        UR                  SS5      n	UR                  T
R                  R                  UUUU	S95        MÑ     U$ 7f)z#
List all tools on the Gradio app.
NÚ	_mcp_typer«   r0  )rä   ÚdescriptionÚinputSchemar2  )r­   ri   rÏ   r¾   r¿   rÀ   rD  Úget_fn_descriptionrê   r©   Úappendr&   ÚTool)rï   r¦   r¯   rÄ   rÅ   rE  rè   Úschemar8   Ú	tool_metarU   s             €rV   Ú
list_toolsÚ5GradioMCPServer.create_mcp_server.<locals>.list_toolsE  sù   øé € ð
 "×AÑAÓCˆNàˆEØ,0×,AÑ,A×,GÑ,GÖ,IÑ(�	Ø!Ñ-°)Ó2QÙà×?Ñ?ÀÓN�àÑ$Ø—{‘{Ñ*ä §¡¨[×9Ñ9Ø$ŸK™K×1Ñ1°VÓ;ñ à*.×*AÑ*AÀ(Ó*VÑ'�Ø ×1Ñ1°)ÓH‘	�Ü# H§K¡K°¸dÓC�	à—‘Ø—J‘J—O‘OØ&Ø$/Ø$*Ø'ð	 $ð öñ' -Jð6 ˆLùs   ƒDDc            
   “   ó¶  >#   • / n T	R                  5       nT	R                  R                  5        GH"  u  p#Ub  X!;  a  M  T	R                  U5      nU(       d  M*  UR                  (       d  M=  [        UR                  S5      (       d  MZ  UR                  R                  S:X  d  Mv  UR                  R                  n[        R                  " SU5      n[        R                  " UR                  5      u  pvnU(       a  MÏ  U R                  T	R                  R                  UUR                  R                  UUR                  R                   S95        GM%     U $ 7f)z
List all available resources.
rD  Úresourceú\{([^}]+)\})Úurirä   rE  ÚmimeType)r­   ri   rÏ   r¾   r¿   rÀ   rD  Ú_mcp_uri_templater´   Úfindallr    Úget_function_descriptionrH  r&   ÚResourcer¼   Ú_mcp_mime_type)
Ú	resourcesrï   r¯   rÄ   rÅ   Úuri_templaterè   rE  r8   rU   s
            €rV   Úlist_resourcesÚ9GradioMCPServer.create_mcp_server.<locals>.list_resourcesj  s  øé € ð
 ˆIà!×AÑAÓCˆNØ,0×,AÑ,A×,GÑ,G×,IÑ(�	Ø!Ñ-°)Ó2QÙà×?Ñ?ÀÓN�ç�HØ ŸŸ™Ü §¡¨[×9Ó9Ø Ÿ™×-Ñ-°Õ;à#+§;¡;×#@Ñ#@�LÜ!#§¢¨N¸LÓ!I�JÜ16×1OÒ1OØ Ÿ™ó2Ñ.�K¨Q÷ &˜:Ø!×(Ñ(Ø ŸJ™J×/Ñ/Ø$0Ø%-§[¡[×%9Ñ%9Ø,7Ø)1¯©×)CÑ)Cð	 0ð ÷ñ# -Jð2 Ðùó&   ƒAEÁEÁ-EÂ
EÂ&AEÃ?AEc            
   “   ó¶  >#   • / n T	R                  5       nT	R                  R                  5        GH"  u  p#Ub  X!;  a  M  T	R                  U5      nU(       d  M*  UR                  (       d  M=  [        UR                  S5      (       d  MZ  UR                  R                  S:X  d  Mv  UR                  R                  n[        R                  " SU5      n[        R                  " UR                  5      u  pvnU(       d  MÏ  U R                  T	R                  R                  UUR                  R                  UUR                  R                   S95        GM%     U $ 7f)z(
List all available resource templates.
rD  rO  rP  )ÚuriTemplaterä   rE  rR  )r­   ri   rÏ   r¾   r¿   rÀ   rD  rS  r´   rT  r    rU  rH  r&   ÚResourceTemplater¼   rW  )
Ú	templatesrï   r¯   rÄ   rÅ   rY  rè   rE  r8   rU   s
            €rV   Úlist_resource_templatesÚBGradioMCPServer.create_mcp_server.<locals>.list_resource_templates�  s  øé € ð
 ˆIØ!×AÑAÓCˆNØ,0×,AÑ,A×,GÑ,G×,IÑ(�	Ø!Ñ-°)Ó2QÙà×?Ñ?ÀÓN�ç�HØ ŸŸ™Ü §¡¨[×9Ó9Ø Ÿ™×-Ñ-°Õ;à#+§;¡;×#@Ñ#@�LÜ!#§¢¨N¸LÓ!I�JÜ16×1OÒ1OØ Ÿ™ó2Ñ.�K¨Q÷ "�zØ!×(Ñ(Ø ŸJ™J×7Ñ7Ø,8Ø%-§[¡[×%9Ñ%9Ø,7Ø)1¯©×)CÑ)Cð	 8ð ÷ñ# -Jð2 Ðùr\  rQ  c              “   ó4  >#   • [        U 5      n [        TR                  5      I Sh  v•N nTR                  R	                  5        GH8  nTR                  U5      nU(       d  M  UR                  (       d  M1  [        UR                  S5      (       d  MN  UR                  R                  S:X  d  Mj  UR                  R                  n[        R                  " SU5      n0 nSnU(       an  [        R                  " U5      nU H  n	UR                  SU	 S3SU	 S	35      nM     [        R                  " S
U S3U 5      n
U
(       a  U
R                  5       nSnOX@:X  a  SnU(       d  GM!  UTR                   S   ;   a.  TR                   S   U   S   n["        R$                  " USU5      nO['        UR	                  5       5      nUR(                  " USU06  Sh  v•N nUR*                  S:X  d  M  UR,                  (       d  M.  UR.                  nUS   S   n  UR                  R0                  nU(       a;  UR3                  S5      (       d%  [4        R6                  " WR9                  S5      5      nTR;                  WUS9/s  $    [=        SU  35      e GNj N¼
 N‚7f)z"
Read a specific resource by URI.
NrD  rO  rP  Fz\{z\}z(?P<z>[^/]+)Ú^Ú$Tr»   rè   ré   r.  r  r  r   ztext/Úascii)rC   Ú	mime_typezResource not found: )rŸ   r   râ   ri   Úvaluesr¾   r¿   rÀ   rD  rS  r´   rT  r“   r‘   ÚmatchÚ	groupdictra   rì   rí   Úlistr8  r  r   r!  rW  Ú
startswithÚbase64Ú	b64decodeÚencoder*   rß   )rQ  r?  rÄ   rÅ   rY  rè   ÚkwargsÚmatchedÚpatternÚparamri  rò   ró   rü   r  rù   rg  rU   s                    €rV   Úread_resourceÚ8GradioMCPServer.create_mcp_server.<locals>.read_resource¯  s[  øé € ô
 �c“(ˆCÜ# D×$>Ñ$>Ó?×?ˆFØ!%×!6Ñ!6×!=Ñ!=×!?�Ø×?Ñ?ÀÓN�÷ �HØ ŸŸ™Ü §¡¨[×9Ó9Ø Ÿ™×-Ñ-°Õ;à#+§;¡;×#@Ñ#@�LÜ!#§¢¨N¸LÓ!I�Jà�FØ#�Gæ!Ü"$§)¢)¨LÓ"9˜Û%/˜EØ&-§o¡oØ"& u g¨TÐ 2°d¸5¸'ÀÐ4Ió'šGñ &0ô !#§¢¨1¨W¨I°Q¨¸Ó =˜Þ Ø%*§_¡_Ó%6˜FØ&*˜GøØ%Ó,Ø"&˜ç‘wØ(¨D¯M©MÐ:KÑ,LÓLØ.2¯m©mÐ<MÑ.NØ -ñ/à*ñ/,˜Oô .:×-HÒ-HØ /Ø "Ø &ó.™Nô .2°&·-±-³/Ó-B˜Nà,2¯MªMØ+ð-Ø6Cò-÷ & &ð  &Ÿ{™{¨hÕ6¸6¿<¿<¹<Ø)/¯© Ø)/°©¸Ñ): Ø %à$,§K¡K×$>Ñ$>˜	Þ$¨Y×-AÑ-AÀ'×-JÑ-JÜ%+×%5Ò%5°f·m±mÀGÓ6LÓ%M˜Fà ×5Ñ5Ø(.¸)ð 6ð ð ò ñi "@ôt Ð3°C°5Ð9Ó:Ð:òw @ñT&ñ -ùs`   ƒ$J§J¨8JÁ$JÁ7JÂJÂ0B2JÅ'A,JÇJÇJÇJÇJÇ.JÈBJÊJÊJc               “   óÖ  >#   • / n TR                  5       nTR                  R                  5        GH+  u  p#Ub  X!;  a  M  TR                  U5      nU(       d  M*  UR                  (       d  M=  [        UR                  S5      (       d  MZ  UR                  R                  S:X  d  Mv  [        R                  " UR                  5      u  pVn[        R                  " UR                  5      nU V	V
Vs/ s H7  u  pš  nTR                  R                  U	UR                  U	S5      U
(       + S9PM9     nn
n	nU R                  TR                  R                  UUUS95        GM.     U $ s  snn
n	f 7f)z
List all available prompts.
rD  Úpromptr5   )rä   rE  Úrequired)rä   rE  rå   )r­   ri   rÏ   r¾   r¿   rÀ   rD  r    rU  Úget_function_paramsr&   ÚPromptArgumentrI   rH  ÚPrompt)Úpromptsrï   r¯   rÄ   rÅ   rE  rè   r8   Úfunction_paramsÚ
param_nameÚhas_defaultrå   rU   s               €rV   Úlist_promptsÚ7GradioMCPServer.create_mcp_server.<locals>.list_promptsò  sM  øé € ð
 ˆGØ!×AÑAÓCˆNØ,0×,AÑ,A×,GÑ,G×,IÑ(�	Ø!Ñ-°)Ó2QÙà×?Ñ?ÀÓN�ç�HØ ŸŸ™Ü §¡¨[×9Ó9Ø Ÿ™×-Ñ-°Õ9ä16×1OÒ1OØ Ÿ™ó2Ñ.�K¨Qô ',×&?Ò&?ÀÇÁÓ&L�Oñ >Mõ!ò >MÑ9˜J°Q¸ð Ÿ
™
×1Ñ1Ø!+Ø(2¯©°zÀ2Ó(FØ)4¤_ð 2ó ñ
 >Mð ò !ð —N‘NØŸ
™
×)Ñ)Ø!*Ø(3Ø&/ð *ð ÷ñ/ -Jð< ˆNùô!ùs+   ƒAE)ÁE)Á-E)Â
E)Â&A	E)Ã/>E"
Ä-<E)Nc              “   ó  >#   • [        T
R                  5      I Sh  v•N nSnT
R                  R                  5        H…  nT
R	                  U5      nU(       d  M  UR
                  (       d  M0  [        UR
                  S5      (       d  MM  UR
                  R                  S:X  d  Mi  UR
                  R                  U :X  d  M…    O   U(       d  [        SU  35      eU=(       d    0 nT
R	                  U5      nUc   eUT
R                  S   ;   a.  T
R                  S   U   S   n[        R                  " USU5      nO[        UR                  5       5      nUR                  " USU06  Sh  v•N nUR                  S	:X  d  M  UR                   (       d  M.  UR"                  nUS
   S   n	  T
R$                  R'                  T
R$                  R)                  ST
R$                  R+                  S[-        W	5      S9S9/S9$  GNâ N”
 NZ7f)z1
Get a specific prompt with filled-in arguments.
NrD  rw  zPrompt not found: r»   rè   ré   r.  r  r  r   ÚuserÚtext©r  r„  )ÚrolerC   )Úmessages)r   râ   ri   rh  r¾   r¿   rÀ   rD  Ú	_mcp_namerß   ra   rì   rí   rk  r8  r  r   r!  r&   ÚGetPromptResultÚPromptMessageÚTextContentrŸ   )rä   rå   r?  rÄ   rÅ   rò   ró   rü   r  rù   rU   s             €rV   Ú
get_promptÚ5GradioMCPServer.create_mcp_server.<locals>.get_prompt  s×  øé € ô $ D×$>Ñ$>Ó?×?ˆFà ˆMØ!%×!6Ñ!6×!=Ñ!=Ö!?�Ø×?Ñ?ÀÓN�ç�HØ ŸŸ™Ü §¡¨[×9Ó9Ø Ÿ™×-Ñ-°Õ9Ø Ÿ™×-Ñ-°Õ5áñ "@ö !Ü Ð#5°d°VÐ!<Ó=Ð=à!Ÿ RˆIà×;Ñ;¸MÓJˆHØÑ'Ð'Ð'à §¡Ð.?Ñ @Ó@Ø"&§-¡-Ð0AÑ"BÀ=Ñ"QØ ñ#�ô ".×!<Ò!<Ø#ØØó"‘ô "& i×&6Ñ&6Ó&8Ó!9�à &§¢¨~Ð VÈÒ V÷ �fØ—;‘; (Õ*¨v¯|¯|©|Ø#Ÿ^™^�FØ# F™^¨AÑ.�FØà—:‘:×-Ñ-à—J‘J×,Ñ,Ø#Ø $§
¡
× 6Ñ 6¸FÌÈVËÐ 6Ð Uð -ð ðð .ð ð òQ @ñDÑ Vùs_   ƒHœG>�9HÁHÁ-HÂ
HÂ&HÃB&HÅ(HÅ,HÅ-HÅ0HÆHÆA)HÈHÈHrt   )r(   rŸ   r_   r  rA  r¨   r   r&   r;  rL  rk  rI  rZ  rV  ra  r_  rt  r   r*   r€  r{  rŒ  r‰  )	rU   ÚserverrA  rL  rZ  ra  rt  r€  rŒ  s	   `        rV   rb   Ú!GradioMCPServer.create_mcp_serverò  sÖ  ø€ ð —‘œS §¡×!2Ñ!2×!B°lÓCÓDˆà	×	Ñ	Ó	ðD	ÜðD	Ü"&¤s¬C x¡.ðD	à�Z‰Z×&Ñ&öD	ó 
ðD	ðL 
×	Ñ	Ó	ð"	¤$ t§z¡z§¡Ñ"7ö "	ó 
ð"	ðH 
×	Ñ	Ó	 ð 	¤d¨4¯:©:×+>Ñ+>Ñ&?ö  	ó 
!ð 	ðD 
×	'Ñ	'Ó	)ð	¬t°D·J±J×4OÑ4OÑ/Pö 	ó 
*ð	ðB 
×	Ñ	Ó	ð@	;¤V¬c¡\ð @	;´d¸4×;TÑ;TÑ6Uö @	;ó 
 ð@	;ðD 
×	Ñ	Ó	ð$	¤D¨¯©×):Ñ):Ñ$;ö $	ó 
ð$	ðL 
×	Ñ	Ó	à:>ñ5	Üð5	Ü"&¤s¬C x¡.°4Ñ"7ð5	à�Z‰Z×'Ñ'÷5	ó 
ð5	ðn ˆrG   r9   Úsubpathrd   c                 ó   ^ ^• SnT R                  U5      mUT l        U U4S jnUR                  U[        [	        ST R
                  S9[	        SUS9[        STR                  S9[        ST R                  S9/S95        g	)
zñ
Launch the MCP server on the SSE transport.

Parameters:
    app: The Gradio app to mount the MCP server on.
    subpath: The subpath to mount the MCP server on. E.g. "/gradio_api/mcp"
    root_path: The root path of the Gradio Blocks app.
z
/messages/c              “   óÐ  >#   •  TR                  U R                  U R                  U R                  5       IS h  v•N nTR                  R                  US   US   TR                  R                  5       5      I S h  v•N   S S S 5      IS h  v•N   [        5       $  Na N  N! , IS h  v•N  (       d  f       N'= f! [         a  n[        S[        U5       35        e S nAff = f7f)Nr   r³   zMCP SSE connection error: )Úconnect_sser<   r=   Ú_sendrc   rZ   Úcreate_initialization_optionsr   Ú	ExceptionÚprintrŸ   )rš   Ústreamsro   rU   Ússes      €€rV   Ú
handle_sseÚ5GradioMCPServer.launch_mcp_on_sse.<locals>.handle_sse`  sÆ   øé € ðØŸ?™?Ø—M‘M 7§?¡?°G·M±M÷ô àØŸ/™/×-Ñ-Ø ™
Ø ™
ØŸ™×EÑEÓGó÷ ð ÷÷ ô  “zÐ!ññ÷÷ ÷ ð ûô ó ÜÐ2´3°q³6°(Ð;Ô<Øûðüs   ƒC&…5B< ºB»B< ¾?B"Á=BÁ>B"ÂB< ÂB ÂB< ÂC&ÂB< ÂB"Â B< Â"B9Â(B+Â)B9Â5B< Â<
C#ÃCÃC#Ã#C&z/schema)Úendpointz/sse)r9   r6   )ÚroutesN)	r2   rd   Úmountr   r   Úget_complete_schemar   Úhandle_post_messagerW   )rU   r9   r�  rd   Úmessages_pathrš  r™  s   `     @rV   Úlaunch_mcp_on_sseÚ!GradioMCPServer.launch_mcp_on_sseS  s„   ù€ ð %ˆØ×%Ñ% mÓ4ˆØ"ˆŒö	ð 	�	‰	ØÜäØ!Ø!%×!9Ñ!9ñô ˜&¨:Ñ6Ü˜,¨C×,CÑ,CÑDÜ˜# 4×#>Ñ#>Ñ?ðñ
õ	
rG   rÄ   zBlockFunction | Nonec                 óx   ^• [        U4S jU R                  R                  R                  5        5       S5      nU$ )zô
Get the BlockFunction for a given endpoint name (e.g. "/predict").

Parameters:
    endpoint_name: The name of the endpoint to get the BlockFunction for.

Returns:
    The BlockFunction for the given endpoint name, or None if it is not found.
c              3   ój   >#   • U  H(  nUR                   TR                  S 5      :X  d  M$  Uv •  M*     g7f)r6   N)r.  rÁ   )rË   r¿   rÄ   s     €rV   rÍ   ÚBGradioMCPServer.get_block_fn_from_endpoint_name.<locals>.<genexpr>‹  s1   øé € ð â2�BØ—;‘; -×"6Ñ"6°sÓ";Ñ;÷ ‘Ú2ùs   ƒ#3ª	3N)Únextr_   Úfnsrh  )rU   rÄ   rÅ   s    ` rV   r¾   Ú/GradioMCPServer.get_block_fn_from_endpoint_name~  s9   ø€ ô ôàŸ+™+Ÿ/™/×0Ñ0Ô2óð
 ó
ˆð ˆrG   c                 ó   • g)ze
Sentence prompting the agent to use the upload_file_to_gradio tool if a file is passed as an input.
zÎ If a user passes a file as an input, use the upload_file_to_gradio tool, if present, to upload the file to the gradio app and create a Gradio File Input. Then use the returned path as the input to the toolré   ru   s    rV   Ú_file_data_tool_descriptionÚ+GradioMCPServer._file_data_tool_description”  s   € ð
 `rG   rÅ   c                 óà  • [         R                  " UR                  5      u  p4nU R                  X$5      u  pgUR                  SL a  SnO�UR                  cW  [        U5      S:”  a  X0R                  -  nU(       a2  UUR                  S5      (       a  SOSS-   SR                  U5      -   -  nO)UR                  n[        U5      S:”  a  X0R                  -  n[        U[        5      (       d   eX44$ )zª
Get the description of a function, which is used to describe the tool in the MCP server.
Also returns the description of each parameter of the function as a dictionary.
Fr5   r   rþ   z
 Returns: z, )r    rU  r¿   rê   Úapi_descriptionÚlenr«  rJ   ÚjoinrÊ   rŸ   )rU   rÅ   r¯   rE  rè   Úreturnsr8   rð   s           rV   rG  Ú"GradioMCPServer.get_fn_description›  sì   € ô ,1×+IÒ+IÈ(Ï+É+Ó+VÑ(ˆ Ø $× 5Ñ 5°iÓ LÑˆØ×#Ñ# uÒ,Ø‰KØ×%Ñ%Ñ-ÜÐ%Ó&¨Ó*Ø×?Ñ?Ñ?�ÞØØ&×/Ñ/°×4Ñ4‘R¸#Ø"ñ#à—i‘i Ó(ñ)ñ�øð #×2Ñ2ˆKÜÐ%Ó&¨Ó*Ø×?Ñ?Ñ?�Ü˜+¤s×+Ñ+Ð+Ð+ØÐ&Ð&rG   rÑ   zComponent | BlockContextr  c                 ó€   • [        U 5       H.  u  p#[        U[        5      (       d  M  UR                  US5        M0     U$ )zy
Insert None placeholder values for any State input components, as State inputs
are not included in the endpoint schema.
N)Ú	enumeraterÊ   r"   Úinsert)rÑ   r  ÚiÚinput_component_types       rV   r3  Ú"GradioMCPServer.insert_empty_state¶  s9   € ô (1°Ö'8Ñ#ˆAÜÐ.´×6Ó6Ø—‘˜A˜tÖ$ñ (9ð ˆrG   Ú
componentsc                 ó~   • [        U 5       H-  u  p#[        U[        5      (       d  M  UR                  U5        M/     U$ )z„
Remove any values corresponding to State output components from the data
as State outputs are not included in the endpoint schema.
)r´  rÊ   r"   rî   )r¹  r  r¶  Úcomponent_types       rV   r9  Ú"GradioMCPServer.pop_returned_stateÃ  s5   € ô "+¨:Ö!6ÑˆAÜ˜.¬%×0Ó0Ø—‘˜–ñ "7ð ˆrG   rè   c           
      óþ  • U R                   R                  U5      nUc  [        SU 35      eU R                  S   nUR                  U5      nUc   eSUS    Vs0 s HB  nUS   0 US   EU(       a  US   U;   a	  SX&S      0O0 ESU;   a  US   (       a  S	US   0O0 E_MD     snS
.nUS    Vs/ s H!  nUR                  SS5      (       a  M  US   PM#     nnU(       a  X‡S'   U R	                  U5      $ s  snf s  snf )aq  
Get the input schema of the Gradio app API, appropriately formatted for MCP.

Parameters:
    tool_name: The name of the tool to get the schema for, e.g. "predict"
    parameters: The description and parameters of the tool to get the schema for.
Returns:
    - The input schema of the Gradio app API.
    - A list of positions of FileData objects in the input schema.
rç   r»   Úobjectrè   Úparameter_namer  rE  Úparameter_defaultÚdefault)r  Ú
propertiesÚparameter_has_defaultFrx  )ri   rI   rß   ra   Úsimplify_filedata_schema)	rU   r¯   rè   rÄ   r»   Úendpoint_infoÚprJ  rx  s	            rV   rê   Ú GradioMCPServer.get_input_schemaÐ  si  € ð ×-Ñ-×1Ñ1°)Ó<ˆØÑ ÜÐAÀ)ÀÐMÓNÐNØŸ-™-Ð(9Ñ:ˆØ'×+Ñ+¨MÓ:ˆØÑ(Ð(Ð(ð ð ' |Ò4óò 5�Að Ð"Ñ#ð &Ø˜‘ið&ö &¨!Ð,<Ñ*=ÀÓ*Kð '¨
Ð5EÑ3FÑ(GÑHàð&ð /°!Ó3¸Ð:M×8Nð # AÐ&9Ñ$:Ñ;àð&ò ñ 5ññ
ˆð* # <Ò0ó
â0�Ø—5‘5Ð0°%×8ó  ˆAÐÔÙ0ð 	ð 
ö
 Ø!)�:ÑØ×,Ñ,¨VÓ4Ð4ùò1ùò"
s   ÁA	C5Â/C:Ã	C:c              ƒ   óØ  #   • U R                   (       d  [        0 5      $ [        [        US0 5      5      nSnSU;   a  US   R	                  S5      n[        U5      nSn/ nU R                  R                  5        GH`  u  pxUb  Xs;  a  M  U R                  U5      n	U	b  U	R                  c   eU R                  X—5      u  p«U R                  X{5      u  pÍ[        U5      S:”  a	  U(       d  Sn[        R                  " U	R                  5      n/ nUR                  5        HR  u  nnU[        L d  U[         [           L d  M!  UR#                  SS	5      R%                  5       nUR'                  U5        MT     S
n[)        U	R                  S5      (       a  U	R                  R*                  nUUU	R,                  S.nU(       a  UUS'   UU
UUS.nUR'                  U5        GMc     [        U5      $ 7f)aZ  
Get the complete schema of the Gradio app API. For debugging purposes, also used by
the Hugging Face MCP server to get the schema for MCP Spaces without needing to
establish an SSE connection.

Parameters:
    request: The Starlette request object.

Returns:
    A JSONResponse containing a dictionary mapping tool names to their input schemas.
r¥   Nr¦   r{   Fr   Tr8   Ú-r«   rD  )Úfile_data_presentÚmcp_typerÄ   rH   )rä   rE  rF  r6  )ra   r   r¨   r©   rf   rÂ   ri   rÏ   r¾   r¿   rG  rê   r¯  r    Úget_type_hintsr#   r	   r‘   rK   rH  rÀ   rD  r.  )rU   rš   r¥   rï   r¦   rÊ  Úschemasr¯   rÄ   rÅ   rE  rè   rJ  rð   Ú
type_hintsÚrequired_headersr~  Ú	type_hintÚheader_namerË  r6  Úinfos                         rV   rŸ  Ú#GradioMCPServer.get_complete_schema  sÝ  é € ð �}�}Ü Ó#Ð#äœG G¨^¸RÓ@ÓAˆØˆØ�lÓ"Ø  Ñ)×/Ñ/°Ó4ˆEÜ  ›ZˆNà!ÐàˆØ(,×(=Ñ(=×(CÑ(C×(EÑ$ˆIØÑ)¨iÓ.MÙØ×;Ñ;¸MÓJˆHØÑ'¨H¯K©KÑ,CÐCÐCà&*×&=Ñ&=¸hÓ&RÑ#ˆKØ)-×)>Ñ)>¸yÓ)UÑ&ˆFÜÐ%Ó&¨Ó*Ö3DØ$(Ð!ä×-Ò-¨h¯k©kÓ:ˆJØ!ÐØ)3×)9Ñ)9Ö);Ñ%�
˜IØ¤Ò&¨)´xÄÑ7GÔ*GØ",×"4Ñ"4°S¸#Ó">×"DÑ"DÓ"F�KØ$×+Ñ+¨KÖ8ñ *<ð
 ˆHÜ�x—{‘{ K×0Ñ0Ø#Ÿ;™;×0Ñ0�ð &7Ø$Ø!)×!2Ñ!2ñˆDö
  Ø"2��Y‘ð "Ø*Ø%Øñ	ˆDð �N‰N˜4× ñI )FôL ˜GÓ$Ð$ùs   ‚D8G*Ä>B,G*rJ  c           	      óú   ^^^• S[         S[        [        [         4   S[        4S jm  S	S[         S[        [        [
        -     S-  S[        [        [         4   S-  S[         4UUU4S jjjm/ mT" U5      nUT4$ )
a}  
Parses a schema of a Gradio app API to identify positions of FileData objects. Replaces them with base64
strings while keeping track of their positions so that they can be converted back to FileData objects
later.

Parameters:
    schema: The original schema of the Gradio app API.

Returns:
    A tuple containing the simplified schema and the positions of the FileData objects.
ÚobjÚdefsr?   c                 ó`  • [        U [        5      (       d  gSU ;   aC  U S   nUR                  S5      (       a'  UR                  S5      S   nUR	                  U0 5      n OgU R	                  S0 5      nUR	                  S0 5      nSU;   aC  US   nUR                  S5      (       a'  UR                  S5      S   nUR	                  U0 5      nOgUR	                  S0 5      R	                  S0 5      nUR	                  S	0 5      R	                  S5      nUR	                  S
5      S:H  =(       d    US:H  $ )NFz$refz#/$defs/r6   r7   rÂ  r6  Ú_typerÁ  Úconstzgradio.FileData)rÊ   r¨   rl  rf   rI   )rÕ  rÖ  ÚrefrQ   Úpropsr6  Ú
type_fieldÚdefault_types           rV   Úis_gradio_filedataÚDGradioMCPServer.simplify_filedata_schema.<locals>.is_gradio_filedataQ  s  € Ü˜c¤4×(Ñ(Øà˜‹}Ø˜&‘k�Ø—>‘> *×-Ñ-ØŸ)™) C›.¨Ñ,�CØŸ(™( 3¨Ó+‘Cà à—G‘G˜L¨"Ó-ˆEØ—9‘9˜V RÓ(ˆDà˜‹~Ø˜6‘l�Ø—>‘> *×-Ñ-ØŸ)™) C›.¨Ñ,�CØŸ8™8 C¨Ó,‘Dà àŸ™ ,°Ó3×7Ñ7¸ÀÓDˆJØŸ8™8 I¨rÓ2×6Ñ6°wÓ?ˆLà—‘˜wÓ'Ð+<Ñ<÷ 5ØÐ#4Ñ4ðrG   NÚnoderA   c                 óü  >• Uc  / nUc  0 n[         R                  " U 5      n [        U [        5      (       aß  SU ;   a  UR	                  U S   5        T
" X5      (       aD  T	R                  UR                  5       5        S H  nU R                  US 5        M     SU S'   SU S'   0 nSU ;   =(       a    SU ;   nU R                  5        HK  u  p6U(       a  US:X  a  T" XaU5      XC'   M  UR                  U5        T" XaU5      XC'   UR                  5         MM     U$ [        U [        5      (       aQ  / n[        U 5       H>  u  pxUR                  U5        UR                  T" X�U5      5        UR                  5         M@     U$ U $ )Nú$defs)rÂ  Úadditional_descriptionrâ  Ústringr  z1Gradio File Input - a http or https url to a fileÚformatrÂ  )
ÚcopyÚdeepcopyrÊ   r¨   rü   rH  rî   rÏ   rk  r´  )rà  rA   rÖ  rQ   rù   Úis_schema_rootrR   r¶  Úitemrð   rÞ  Útraverses            €€€rV   rê  Ú:GradioMCPServer.simplify_filedata_schema.<locals>.traverseo  sV  ø€ ð
 ‰|Ø�Ø‰|Ø�ä—=’= Ó&ˆDä˜$¤×%Ñ%Ø˜d“?Ø—K‘K  W¡Ô.á% d×1Ñ1Ø&×-Ñ-¨d¯i©i«kÔ:ÛP˜ØŸ™  dÖ+ñ  Qà#+�D˜‘LØ%X�D˜‘Nà�Ø!'¨4¡×!H°LÀDÑ4H�Ø"&§*¡*¦,‘J�CÞ%¨#°Ó*=Ù&.¨u¸DÓ&A˜›àŸ™ CÔ(Ù&.¨u¸DÓ&A˜™ØŸ™ž
ñ #/ð �ä˜D¤$×'Ñ'Ø�Ü(¨ž‘G�AØ—K‘K ”NØ—M‘M¡(¨4°tÓ"<Ô=Ø—H‘H–Jñ  /ð �àˆKrG   ©NN)r   r¨   rŸ   Úboolrk  Úint)rU   rJ  Úsimplified_schemarð   rÞ  rê  s      @@@rV   rÄ  Ú(GradioMCPServer.simplify_filedata_schemaB  s–   ú€ ð	¤Cð 	¬t´C¼°H©~ð 	Ä$ô 	ð@ ,0Ø*.ñ*	Üð*	ä”sœS‘y‘/ DÑ(ð*	ô ”sœC�x‘. 4Ñ'ð*	ô ÷	*	ò *	ðX 57ÐÙ$ VÓ,ÐØ Ð"4Ð4Ð4rG   rR   rð   c                 ór   ^^• SS[         S[        [        [        -     S-  S[         4UU4S jjjmT" U5      $ )aŸ  
Convert specific string values back to FileData objects based on their positions.
This is used to convert string values (as base64 encoded strings) to FileData
dictionaries so that they can be passed into .preprocess() logic of a Gradio app.

Parameters:
    value: The input data to process, which can be an arbitrary nested data structure
        that may or may not contain strings that should be converted to FileData objects.
    filedata_positions: List of paths to positions in the input data that should be converted to FileData objects.

Returns:
    The processed data with strings converted to FileData objects where appropriate. Base64
    encoded strings are first saved to a temporary file and then converted to a FileData object.

Example:
    >>> convert_strings_to_filedata(
        {"image": "data:image/jpeg;base64,..."},
        [["image"]]
    )
    >>> {'image': FileData(path='<temporary file path>')},
Nrà  rA   r?   c           
      ó:  >• Uc  / n[        U [        5      (       a0  U R                  5        VVs0 s H  u  p#UT" X1U/-   5      _M     snn$ [        U [        5      (       a*  [	        U 5       VVs/ s H  u  pET" XQU/-   5      PM     snn$ [        U [
        5      (       ao  UT;   ai  U R                  S5      (       a$  [        [        R                  " U [        5      5      $ U R                  S5      (       a  [        U 5      $ [        SU  35      eU $ s  snnf s  snnf )Nzdata:)zhttp://zhttps://zSInvalid file data format, provide a url ('http://...' or 'https://...'). Received: )rÊ   r¨   rÏ   rk  r´  rŸ   rl  r   r   Úsave_base64_to_cacheÚDEFAULT_TEMP_DIRrß   )rà  rA   rQ   rR   r¶  ré  rð   rê  s         €€rV   rê  Ú=GradioMCPServer.convert_strings_to_filedata.<locals>.traverse¸  s  ø€ Ø‰|Ø�ä˜$¤×%Ñ%àIMÏÉÌôÚIU¹:¸3�C™ %°°©Ó6Ò6Éòð ô ˜D¤$×'Ñ'ÜBKÈDÄ/ÔRÂ/±w°q™ ¨q¨c¡zÖ2Á/ÒRÐRÜ˜D¤#×&Ñ&¨4Ð3EÓ+EØ—?‘? 7×+Ñ+ô 'Ü(×=Ò=¸dÔDTÓUóð ð —_‘_Ð%<×=Ñ=Ü& tÓ,Ð,ä$ØmÐnrÐmsÐtóð ð ˆKùó%ùó Ss   ¯DÁ/Drt   )r   rk  rŸ   rî  )rU   rR   rð   rê  s     `@rV   rë   Ú+GradioMCPServer.convert_strings_to_filedataŸ  s;   ù€ ñ2	œ3ð 	¤d¬3´©9¡o¸Ñ&<ð 	Ì÷ 	ñ 	ñ2 ˜‹ÐrG   Ú	file_pathc                 ó.  • [         R                  R                  U 5      (       d  g[         R                  R                  U R	                  5       5      S   nU[
        R                  " 5       ;  a  g [
        R                  " U 5      $ ! [         a     gf = f)zU
If a filepath is a valid image, returns a PIL Image object. Otherwise returns None.
Nr³   )	ÚosrA   ÚexistsÚsplitextrK   r   Úregistered_extensionsÚopenr–  )r÷  Úexts     rV   Ú	get_imageÚGradioMCPServer.get_imageÓ  st   € ô
 �w‰w�~‰~˜i×(Ñ(ØÜ�g‰g×Ñ˜yŸ™Ó0Ó1°!Ñ4ˆØ”e×1Ò1Ó3Ó3Øð	Ü—:’:˜iÓ(Ð(øÜó 	Ùð	ús   Á1B Â
BÂBÚ	file_datac                 ó  • [        U [        5      (       ap  U R                  S5      =n(       aX  [        U[        5      (       aB  UR	                  S5      (       a,  [        UR                  SS5      S   5      R                  5       $ gg)zƒ
If a file_data is a valid FileDataDict with a url that is a data:image/svg+xml, returns bytes of the svg. Otherwise returns None.
r    zdata:image/svgr{   r³   N)rÊ   r¨   rI   rŸ   rl  r   rf   ro  )r  r    s     rV   Úget_svgÚGradioMCPServer.get_svgâ  sj   € ô
 �i¤×&Ñ&°9·=±=ÀÓ3GÐ,G¨CÕ,GÜ˜#œs×#Ñ#¨¯©Ð7G×(HÑ(HÜ˜sŸy™y¨¨aÓ0°Ñ3Ó4×;Ñ;Ó=Ð=ààrG   Úimagerå  c                 óš   • [        5       nU R                  X!S9  [        R                  " UR	                  5       5      R                  S5      $ )z/
Returns a base64 encoded string of the image.
)rå  úutf-8)r   Úsaverm  Ú	b64encodeÚgetvaluer   )r  rå  Úbuffers      rV   Úget_base64_dataÚGradioMCPServer.get_base64_dataï  s<   € ô
 “ˆØ�
‰
�6ˆ
Ñ)Ü×Ò §¡Ó 1Ó2×9Ñ9¸'ÓBÐBrG   rá   z&types.TextContent | types.ImageContentc                 óô  • / n[         R                  " XS5      nU GH×  nU R                  U5      =n(       aœ  [        R                  " U5      R                  S5      nSn[         R                  " XTS    [        5      n[        R                  " U5      n	U SU	 3n
U R                  R                  SXgS9U R                  R                  SS	U
 3S
9/nGO[        R                  " U5      (       aÏ  U R                  US   5      =n(       a€  UR                  =(       d    SnU R!                  XÍ5      nSUR#                  5        3nU R                  R                  SXgS9U R                  R                  SSUS   =(       d    US    3S
9/nOXU R                  R                  S[%        US   =(       d    US   5      S
9/nO$U R                  R                  S[%        U5      S
9/nUR'                  U5        GMÚ     U$ )z¤
Postprocess the output data from the Gradio app to convert FileData objects back to base64 encoded strings.

Parameters:
    data: The output data to postprocess.
Nr  zimage/svg+xmlÚ	orig_namez/gradio_api/file=r  )r  r  rR  r„  zSVG Image URL: r…  rA   Úpngzimage/zImage URL: r    )r   Úadd_root_urlr  rm  r	  r   Úsave_bytes_to_cacherô  rì   Úencode_file_pathr&   ÚImageContentr‹  Úis_file_obj_with_metarÿ  rå  r  rK   rŸ   Úextend)rU   r  rá   Úreturn_valuesr  Ú	svg_bytesÚbase64_dataÚmimetypeÚsvg_pathÚencoded_pathÚsvg_urlÚreturn_valuer  Úimage_formats                 rV   r:  Ú'GradioMCPServer.postprocess_output_dataø  s
  € ð ˆÜ×,Ò,¨T¸TÓBˆÜˆFØ ŸL™L¨Ó0Ð0ˆyÕ0Ü$×.Ò.¨yÓ9×@Ñ@ÀÓI�Ø*�Ü+×?Ò?Ø¨Ñ"5Ð!6Ô9Ió�ô  ,×<Ò<¸XÓF�Ø%˜JÐ&7¸°~ÐF�à—J‘J×+Ñ+Ø$¨;ð ,ð ð —J‘J×*Ñ*Ø#Ø.¨w¨iÐ8ð +ð ð	 ’ô ×3Ò3°F×;Ñ;Ø ŸN™N¨6°&©>Ó:Ð:�5Õ:Ø#(§<¡<×#8°5�LØ"&×"6Ñ"6°uÓ"K�KØ!'¨×(:Ñ(:Ó(<Ð'=Ð>�HàŸ
™
×/Ñ/Ø!(¨{ð 0ð ð Ÿ
™
×.Ñ.Ø!'Ø#.¨v°e©}×/NÀÀvÁÐ.OÐ!Pð /ð ð	$‘Lð Ÿ
™
×.Ñ.Ø!'¬c°&¸±-×2QÀ6È&Á>Ó.Rð /ð ð$‘Lð !%§
¡
× 6Ñ 6¸FÌÈVËÐ 6Ð UÐV�Ø× Ñ  ×.ñQ ðR ÐrG   )rl   rk   ra   r_   rW   r[   rT   rc   rd   rg   ri   )r_   r,   )r?   N)r?   r(   rt   )@r¼   Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r]   r&   Ú
mcp.serverr(   Ú mcp.server.lowlevel.helper_typesr*   Úmcp.server.sser2   Ú"mcp.server.streamable_http_managerr3   r^   rq   ÚpropertyrŸ   rv   Ústaticmethodr   rL   rM   r   r¢   rk  r­   rÂ   r¸   r¨   rh   rj   r   râ   r   Útuplerõ   rú   r   r  rî  r)  rb   r   r¢  r¾   r«  rG  r   r3  r9  rê   r   rŸ  rÄ  rë   r   rÿ  Úbytesr  r  r:  Ú__static_attributes__ré   rG   rV   r1   r1   _  s#  † ñðÝÝ%ÝIÝ5ÝSôH=ðT ð˜3 ™:ó ó ðð ð. eð .°ó .ó ð.ð< ð
 sð 
¨só 
ó ð
ð4% gð %°#ô %ð°°c±¸TÑ1Aô ð ðØðØ-0°©Xðà	óó ðð"  d¨3°¨8¡nô  ô2ð% vô %ð0-HØð-HØ$(¨¨c¨¡Nð-Hà	ˆs�D˜‘I˜t C¨ H™~¨Ð>Ñ	?ô-Hð^¸ð ÀÀSÁ	ô ð ð¨ð ¸#À¹*ó ó ðð>,Øð,Ø(+¨c©	ð,à	ˆc�3ˆh‰ô,ô\_ðB)
 Yð )
¸ð )
Èð )
ÐQUô )
ðVØ ðà	ôð, ð`¨Só `ó ð`ð'Ø'ð'Ø47ð'à	ˆs�D˜˜c˜‘NÐ"Ñ	#ô'ð6 ð
ØÐ3Ñ4ð
Ø<@ð
à	ó
ó ð
ð ð
ØÐ7Ñ8ð
Ø@Cð
à	ó
ó ð
ð -1ñ05àð05ð ˜˜c˜‘N TÑ)ð05ð 
ˆt�C˜�H‰~˜t D¨¨s©¡OÑ4Ð4Ñ	5õ	05ðd>%°Lô >%ð@[5Ø˜3 ˜8‘nð[5à	ˆt�C˜�H‰~˜t D¨¨s©¡OÑ4Ð4Ñ	5ô[5ðz2Øð2Ø.2°4¸¸c¹	±?Ñ.Cð2à	ô2ðh ð˜Sð  U§[¡[°4Ñ%7ó ó ðð ð
˜3ð 
 5¨4¡<ó 
ó ð
ð ðC˜uŸ{™{ð C°Cð C¸Có Có ðCð4Øð4Ø#&ð4à	Ð6Ñ	7÷4øðO$ ó Úðús   ŠH ÈHÈHr1   rY  rE  rg  c                 ó   ^ ^^• UUU 4S jnU$ )z0Decorator to mark a function as an MCP resource.c                 óR   >• SU l         TU l        TU l        T=(       d    SU l        U $ )NrO  z
text/plain)rD  rS  Ú_mcp_descriptionrW  )r¿   rE  rg  rY  s    €€€rV   Ú	decoratorÚresource.<locals>.decorator9  s,   ø€ Ø!ˆŒØ+ˆÔØ)ˆÔØ%×5¨ˆÔØˆ	rG   ré   )rY  rE  rg  r1  s   ``` rV   rO  rO  4  s   ú€ ÷
ð ÐrG   rä   c                 ó   ^ ^• UU 4S jnU$ )z.Decorator to mark a function as an MCP prompt.c                 óX   >• SU l         T=(       d    U R                  U l        TU l        U $ )Nrw  )rD  r¼   rˆ  r0  )r¿   rE  rä   s    €€rV   r1  Úprompt.<locals>.decoratorF  s'   ø€ ØˆŒØ×*˜rŸ{™{ˆŒØ)ˆÔØˆ	rG   ré   )rä   rE  r1  s   `` rV   rw  rw  C  s   ù€ öð ÐrG   Ústructured_outputr2  c                 ó    ^ ^^^• UUU U4S jnU$ )aë  
Decorator to mark a function as an MCP tool (optional, since functions are registered as tools by default).
Can be used to configure various aspects of the tool.

Parameters:
    name: The name of the tool. Overrides the default name of the function.
    description: The description of the tool. Overrides the default description from the function's docstring.
    structured_output: Whether the tool should return structured output (implementation is quite limited at the moment). If True, the output will be wrapped in a dictionary with the key "result" and the value being the output of the function. Recommended to keep this False unless you have a specific reason to need the structured output.
    _meta: Additional metadata for the tool.
c                 óN   >• SU l         TU l        TU l        TU l        TU l        U $ )Nr«   )rD  rˆ  r/  r0  r0  )r¿   r2  rE  rä   r6  s    €€€€rV   r1  Útool.<locals>.decorator`  s-   ø€ ØˆŒØˆŒØ$5ˆÔ!Ø)ˆÔØˆŒØˆ	rG   ré   )rä   rE  r6  r2  r1  s   ```` rV   r«   r«   O  s   û€ ÷"ð ð ÐrG   rì  )NNFN)Vrm  rm   ræ  r’   rŽ   rù  r´   ÚtempfilerÒ   Úcollections.abcr   r   Úior   Úpathlibr   Útypingr   r   r	   r
   Úurllib.parser   r   Úgradio_client.utilsr    rì   r�   Úanyio.to_threadr   Úgradio_clientr   r   r   r   ÚPILr   Úpydanticr   Ústarlette.applicationsr   Ústarlette.requestsr   Ústarlette.responsesr   r   r   Ústarlette.routingr   r   Ústarlette.typesr   r   r   r/   r   r   Úgradio.blocksr!   Úgradio.componentsr"   Úgradio.route_utilsr#   Úgradio.state_holderr$   r]   r&   r%  r(   r&  r*   r+   r,   r-   ÚenvironrI   rŸ   Ú
gettempdirrô  r�   r1   rO  rw  rí  r¨   r«   ré   rG   rV   Ú<module>rP     sb  ðÛ Û Û Û Û Û 	Û 	Û Û ß 3Ý Ý ß 5Ó 5ß *å *Û Ý $ß -ß 4Ý Ý Ý ,Ý &ß DÑ Dß *ß 0Ñ 0ç 7Ñ 7Ý 'Ý #Ý %Ý ,æÝÝ!ÝEç2Ý+ð —:‘:—>‘>Ð"3Ó4÷ ¹Ùˆ×	Ò	Ó	Ó (Ñ*ó9Ð ðgÐ ÷T	Mñ Mðl& PTñØðØ$'¨$¡JðØBEÈÁ*õñ	��t‘ð 	°°t±õ 	ð Ø"Ø#Ø#'ñ	Ø
�‰*ðà�t‘ðð ðð ��S�‰>˜DÑ ö	rG   