Commit Graph
4 Commits
Author SHA1 Message Date
Ethan 5cbf086517 feat: send tool result media to providers like user files (#31383)
Part of [CODAGT-1430](https://linear.app/codercom/issue/CODAGT-1430).
This is the first of four stacked PRs.

Today a PDF in an MCP tool result reaches no provider. On Anthropic we
strip it in chatd. On OpenAI Chat Completions fantasy swaps it for a
"cannot be displayed" note, and on Responses it sends a placeholder that
points at a user message it never sends.

coder/fantasy#73 and coder/fantasy#74 make fantasy convert tool result
media with the converter it already uses for user files: an Anthropic
document inside `tool_result`, an `input_file` inside
`function_call_output` on Responses, and a user file part straight after
the tool message on Chat Completions. Both have merged, and this PR pins
`charm.land/fantasy` to the coder_2_33 tip that has them. Chat
Completions file parts now refuse text files, as Responses already did,
so `AcceptsFilePartMediaType` follows.

The converters take a filename, so an MCP embedded resource blob is now
named after the last element of its URI path, and we store the name with
the tool result so later turns send the same one. OpenAI picks the file
type from the extension, so we append the right one when it's missing or
wrong. We drop names with control characters, as Postgres `jsonb`
rejects NUL, and we redact the name like the rest of the tool response,
since a URI like `https://host/sse/<token>` would otherwise put the
token in the chat history.

There's also a test proving aibridge's Chat Completions re-encode keeps
a user file part, as that's where these PDFs end up.

Anthropic still strips tool result PDFs until the next PR in the stack.
2026-11-11 00:06:40 +11:00
Ethan 15a160aec9 feat: serve internal MCP servers to chats in process (#29828)
This lets coderd give a chat MCP tools that it serves itself, in
process. The first user will be an in-process integration, say a Slack
bot. Nothing uses it yet.

coderd now takes a list of internal MCP servers when it's built, each
with a slug and a `ServerForChat` function. At every step, chatd asks
each one for a server for the chat and connects to it over an in-memory
transport, so the server knows which chat it's serving without any
headers. Nothing is stored on the chat, so the public API doesn't
change, and the chat owner can't remove an internal server.

As only coderd can add one, internal servers ignore the
`chat-inline-mcp-servers` experiment and the
`--disable-chat-caller-supplied-tools` kill switch. Their tools come
straight after the built-in tools, so they win any name clash with
admin, workspace or caller tools. Admin and workspace tools now follow
the same first-wins rule, so the model never sees two tools with the
same name. If `ServerForChat` returns an error, we fail the turn, as we
do when any other read during turn setup fails.

I've put this on top of #30730 and #30731 in the stack. It doesn't need
anything from them, but the Slack bot needs all three.
2026-10-23 12:53:40 +11:00
Ethan e0dce46087 test: fix TestConnectInline_RedactsSensitiveValues on Windows (#30028)
Closes https://linear.app/codercom/issue/CODAGT-1203

`TestConnectInline_RedactsSensitiveValues` fails every time on Windows
in the [nightly
gauntlet](https://github.com/coder/coder/actions/runs/36376854646/job/108784351863).
It asserted on `connection refused`, but Windows words a refused dial
differently. PR CI only runs the `cli` and `agent` packages on Windows,
so we didn't catch it there.

To fix, the test now uses a private IP that the dial guard rejects
before dialling, so the error is the same on every OS and there's no
closed port for another test to grab.

Whilst here, I made production build the persisted connect error through
the same function the truncation test uses, rather than its own copy.
2026-09-29 17:19:42 +10:00
Ethan 016df8cbab feat(coderd/x/chatd/mcpclient): add hardened connect path for inline MCP servers (#29518)
Third PR in the inline MCP servers stack, on top of
https://github.com/coder/coder/pull/29517. This adds the `mcpclient`
side that chatd will use to talk to servers a chat owner declared by
value on the chat. Nothing in chatd calls the new functions yet.

`mcpclient` now owns its input type. `ConnectAll` and the new
`ConnectInline` both take `[]mcpclient.Server`, and `ServerFromConfig`
converts an org-configured row at the boundary, so a caller never has to
fake a `database.MCPServerConfig` for a server that has no row. The org
path behaves as before with two exceptions: `connectAllWithHooks` no
longer skips disabled servers (the call site in `generation_preparer.go`
filters them), and a `custom_headers` row with malformed JSON or an
unknown auth type used to connect without those headers, whereas now
conversion fails and the caller skips the server with a warning.

Org-configured servers were set up by an admin, so we don't put many
limits on them. Inline ones are supplied by whoever created the chat, so
`ConnectInline` treats the server as untrusted. HTTP response bodies are
capped at 1 MiB, a server can expose at most 64 tools with each
definition under 64 KiB and 256 KiB in total, and tool results are
capped at 256 KiB. If a server blows the tool caps we skip the whole
server rather than hand the model a partial tool set, and an oversized
result comes back to the model as a tool error rather than being
truncated silently. Everything the server sends that we log or hand to
the model (tool names, descriptions, schemas, results, connect errors)
has the values in `Server.SensitiveValues` replaced with `[REDACTED]`;
chatd sets that to the URL and header values. Header names aren't
redacted, as they're public and short enough that redacting them would
rewrite ordinary text. For the same reason the redactor ignores anything
shorter than `MinSensitiveValueBytes` (8), and #29546 rejects header
values that short at the API. The slug is caller-supplied too, so it
also goes through the redactor wherever it lands in a tool name, connect
summary or log field.

`AppendInline` merges the resulting tools into an existing tool list
with the existing names winning, so an inline server can't shadow a
builtin or org-configured tool. Collisions are logged once per server
with the `config_id`.

Lastly, every `tools/call` (for both kinds of server) now carries the
model's tool call ID in `_meta` under `com.coder/tool_call_id`. It's a
correlation ID that's stable across chatd retries of the same call, so a
server can use it to dedupe if it wants to. We make no other idempotency
promise.


Part of CODAGT-1131
2026-09-25 18:51:54 +10:00