mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-25 09:51:28 +00:00
* feat: add optional Streamable HTTP for External MCP Serve the existing catalog tools and approval path on a loopback HTTP endpoint so clients that cannot spawn the stdio launcher, such as Codex in WSL, can connect with a bearer token. The listener stays off until the user enables it. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix: close HTTP sockets before stop and export the Codex bearer Destroy tracked connections before awaiting server.close so disable does not hang on a half-open request. Prefix the Codex snippet with export so the child process receives the token. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix: advertise a real client host and return 413 intact Wildcard binds no longer copy 127.0.0.1 into client configs; the URL stays empty until a reachable client address is set. Oversized POSTs receive a JSON-RPC 413 before the socket is closed. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix: recheck the bearer token after the HTTP body arrives A half-open POST already passed auth before readBody. Recheck the live token before dispatch so a rotation during that wait cannot still run a tool. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix: expire idle Streamable HTTP sessions JSON responses do not keep a socket open, so a client that never sends DELETE used to pin an MCP session until the listener stopped. Sweep sessions that have been idle for 15 minutes, and skip any session with a request still in flight. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix: return 404 when an HTTP session id is unknown GET and DELETE with a nonempty session id that is no longer active now return 404, matching POST and the Streamable HTTP session contract. A missing session id stays 400. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix: accept bracketed IPv6 origins Node reports URL.hostname as [::1], so an otherwise valid loopback or wildcard Origin was rejected. Compare the address with the brackets removed. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> * fix: release rejected HTTP handshakes that never open a session A 406 or 415 from the SDK returns normally before onsessioninitialized. Detach the approval client and close the MCP server when that happens so rejected initializes do not accumulate. Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com> --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
324 lines
12 KiB
TypeScript
324 lines
12 KiB
TypeScript
import React, { useState } from "react";
|
|
import {
|
|
buildClaudeHttpAddCommand,
|
|
buildCodexHttpEnvLine,
|
|
buildCodexHttpToml,
|
|
buildExternalMcpHttpUrl,
|
|
buildGenericHttpMcpJson,
|
|
DEFAULT_EXTERNAL_MCP_HTTP_PORT,
|
|
} from "../../../../domain/externalMcpHttp";
|
|
import { useI18n } from "../../../../application/i18n/I18nProvider";
|
|
import { cn } from "../../../../lib/utils";
|
|
import { Button } from "../../../ui/button";
|
|
import { SettingCard, SettingRow, Toggle } from "../../settings-ui";
|
|
import { CopyableCodeBlock } from "./CopyableCodeBlock";
|
|
|
|
export type ExternalMcpHttpViewStatus = {
|
|
enabled?: boolean;
|
|
listening?: boolean;
|
|
port?: number;
|
|
listenAddress?: string;
|
|
allowCustomBind?: boolean;
|
|
clientHost?: string;
|
|
url?: string | null;
|
|
bearerToken?: string | null;
|
|
state?: string;
|
|
error?: string | null;
|
|
errorCode?: string | null;
|
|
};
|
|
|
|
export type ExternalMcpHttpViewSettings = {
|
|
enabled: boolean;
|
|
port: number;
|
|
allowCustomBind: boolean;
|
|
listenAddress: string;
|
|
clientHost: string;
|
|
};
|
|
|
|
type ExternalMcpHttpSectionProps = {
|
|
externalMcpEnabled: boolean;
|
|
settings: ExternalMcpHttpViewSettings;
|
|
status: ExternalMcpHttpViewStatus | null;
|
|
copied: string | null;
|
|
rotating: boolean;
|
|
onCopy: (key: string, text: string) => void;
|
|
onChange: (next: ExternalMcpHttpViewSettings) => void;
|
|
onRotateToken: () => void;
|
|
};
|
|
|
|
function isWildcardAddress(value: string): boolean {
|
|
const address = value.trim();
|
|
return address === "0.0.0.0" || address === "::";
|
|
}
|
|
|
|
function isIpAddress(value: string): boolean {
|
|
const address = value.trim();
|
|
const ipv4 = address.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
|
|
if (ipv4) {
|
|
return ipv4.slice(1).every((part) => Number(part) <= 255 && String(Number(part)) === part);
|
|
}
|
|
return address.includes(":") && /^[0-9a-fA-F:]+$/.test(address);
|
|
}
|
|
|
|
function parsePort(value: string): number | null {
|
|
if (!/^\d+$/.test(value.trim())) return null;
|
|
const port = Number(value);
|
|
if (!Number.isInteger(port) || port < 1024 || port > 65535) return null;
|
|
return port;
|
|
}
|
|
|
|
export const ExternalMcpHttpSection: React.FC<ExternalMcpHttpSectionProps> = ({
|
|
externalMcpEnabled,
|
|
settings,
|
|
status,
|
|
copied,
|
|
rotating,
|
|
onCopy,
|
|
onChange,
|
|
onRotateToken,
|
|
}) => {
|
|
const { t } = useI18n();
|
|
const [portDraft, setPortDraft] = useState(String(settings.port || DEFAULT_EXTERNAL_MCP_HTTP_PORT));
|
|
const [addressDraft, setAddressDraft] = useState(settings.listenAddress || "0.0.0.0");
|
|
const [clientHostDraft, setClientHostDraft] = useState(settings.clientHost || "");
|
|
const [portError, setPortError] = useState(false);
|
|
const [addressError, setAddressError] = useState(false);
|
|
const [clientHostError, setClientHostError] = useState(false);
|
|
|
|
const listenAddress = settings.allowCustomBind
|
|
? (status?.listenAddress || settings.listenAddress)
|
|
: "127.0.0.1";
|
|
const advertisedHost = status?.clientHost ?? settings.clientHost;
|
|
const url = status
|
|
? (status.url || "")
|
|
: buildExternalMcpHttpUrl(listenAddress, settings.port, advertisedHost);
|
|
const showClientHost = settings.allowCustomBind && (
|
|
isWildcardAddress(settings.listenAddress) || isWildcardAddress(addressDraft)
|
|
);
|
|
const token = status?.bearerToken || "";
|
|
const listening = Boolean(status?.listening);
|
|
const statusLabel = listening
|
|
? t("ai.externalMcp.http.listening")
|
|
: t("ai.externalMcp.http.notListening");
|
|
const statusClass = status?.errorCode
|
|
? "text-destructive"
|
|
: listening
|
|
? "text-emerald-500"
|
|
: "text-muted-foreground";
|
|
|
|
let errorText = "";
|
|
if (portError) errorText = t("ai.externalMcp.http.invalidPort");
|
|
else if (addressError) errorText = t("ai.externalMcp.http.invalidAddress");
|
|
else if (clientHostError) errorText = t("ai.externalMcp.http.invalidClientHost");
|
|
else if (status?.errorCode === "client_host_required") errorText = t("ai.externalMcp.http.clientHostRequired");
|
|
else if (status?.errorCode === "port_in_use") {
|
|
errorText = t("ai.externalMcp.http.portInUse", { port: String(status.port ?? settings.port) });
|
|
} else if (status?.errorCode === "invalid_address") errorText = t("ai.externalMcp.http.invalidAddress");
|
|
else if (status?.errorCode === "invalid_port") errorText = t("ai.externalMcp.http.invalidPort");
|
|
else if (status?.errorCode === "listen_failed") errorText = status.error || t("ai.externalMcp.http.listenFailed");
|
|
|
|
const commitPort = () => {
|
|
const port = parsePort(portDraft);
|
|
if (port == null) {
|
|
setPortError(true);
|
|
return;
|
|
}
|
|
setPortError(false);
|
|
setPortDraft(String(port));
|
|
if (port !== settings.port) onChange({ ...settings, port });
|
|
};
|
|
|
|
const commitClientHost = () => {
|
|
const clientHost = clientHostDraft.trim();
|
|
if (clientHost && (!isIpAddress(clientHost) || isWildcardAddress(clientHost))) {
|
|
setClientHostError(true);
|
|
return;
|
|
}
|
|
setClientHostError(false);
|
|
setClientHostDraft(clientHost);
|
|
if (clientHost !== settings.clientHost) onChange({ ...settings, clientHost });
|
|
};
|
|
|
|
const commitAddress = () => {
|
|
const address = addressDraft.trim();
|
|
if (!isIpAddress(address)) {
|
|
setAddressError(true);
|
|
return;
|
|
}
|
|
setAddressError(false);
|
|
setAddressDraft(address);
|
|
if (address !== settings.listenAddress) onChange({ ...settings, listenAddress: address });
|
|
};
|
|
|
|
return (
|
|
<div className="space-y-2">
|
|
<div className="flex items-center justify-between gap-3">
|
|
<div className="text-sm font-semibold text-foreground">{t("ai.externalMcp.http.title")}</div>
|
|
<div className={cn("text-xs font-medium", statusClass)}>{statusLabel}</div>
|
|
</div>
|
|
<p className="text-xs text-muted-foreground leading-5">{t("ai.externalMcp.http.description")}</p>
|
|
<p className="text-xs text-muted-foreground leading-5">{t("ai.externalMcp.http.legacySseNote")}</p>
|
|
<SettingCard divided className="rounded-md border-border/60 bg-background/70">
|
|
<SettingRow
|
|
anchorId="ai-external-mcp-http"
|
|
label={t("ai.externalMcp.http.enable")}
|
|
>
|
|
<Toggle
|
|
checked={settings.enabled}
|
|
onChange={(enabled) => onChange({ ...settings, enabled })}
|
|
ariaLabel={t("ai.externalMcp.http.enable")}
|
|
/>
|
|
</SettingRow>
|
|
<SettingRow
|
|
label={t("ai.externalMcp.http.port")}
|
|
description={t("ai.externalMcp.http.portHint")}
|
|
>
|
|
<input
|
|
type="number"
|
|
aria-label={t("ai.externalMcp.http.port")}
|
|
min={1024}
|
|
max={65535}
|
|
value={portDraft}
|
|
onChange={(event) => {
|
|
setPortDraft(event.currentTarget.value);
|
|
setPortError(false);
|
|
}}
|
|
onBlur={commitPort}
|
|
onKeyDown={(event) => {
|
|
if (event.key === "Enter") {
|
|
event.currentTarget.blur();
|
|
}
|
|
}}
|
|
className="w-24 rounded-md border border-border/60 bg-background px-2 py-1 text-sm"
|
|
/>
|
|
</SettingRow>
|
|
<SettingRow
|
|
label={t("ai.externalMcp.http.allowCustomBind")}
|
|
description={t("ai.externalMcp.http.allowCustomBind.description")}
|
|
>
|
|
<Toggle
|
|
checked={settings.allowCustomBind}
|
|
onChange={(allowCustomBind) => onChange({ ...settings, allowCustomBind })}
|
|
ariaLabel={t("ai.externalMcp.http.allowCustomBind")}
|
|
/>
|
|
</SettingRow>
|
|
{settings.allowCustomBind ? (
|
|
<SettingRow label={t("ai.externalMcp.http.listenAddress")}>
|
|
<input
|
|
type="text"
|
|
aria-label={t("ai.externalMcp.http.listenAddress")}
|
|
placeholder={t("ai.externalMcp.http.listenAddress.placeholder")}
|
|
value={addressDraft}
|
|
onChange={(event) => {
|
|
setAddressDraft(event.currentTarget.value);
|
|
setAddressError(false);
|
|
}}
|
|
onBlur={commitAddress}
|
|
className="w-36 rounded-md border border-border/60 bg-background px-2 py-1 font-mono text-sm"
|
|
/>
|
|
</SettingRow>
|
|
) : null}
|
|
{showClientHost ? (
|
|
<SettingRow
|
|
label={t("ai.externalMcp.http.clientHost")}
|
|
description={t("ai.externalMcp.http.clientHost.description")}
|
|
>
|
|
<input
|
|
type="text"
|
|
aria-label={t("ai.externalMcp.http.clientHost")}
|
|
placeholder={t("ai.externalMcp.http.clientHost.placeholder")}
|
|
value={clientHostDraft}
|
|
onChange={(event) => {
|
|
setClientHostDraft(event.currentTarget.value);
|
|
setClientHostError(false);
|
|
}}
|
|
onBlur={commitClientHost}
|
|
className="w-36 rounded-md border border-border/60 bg-background px-2 py-1 font-mono text-sm"
|
|
/>
|
|
</SettingRow>
|
|
) : null}
|
|
</SettingCard>
|
|
{settings.allowCustomBind ? (
|
|
<p className="text-xs text-amber-500 leading-5">{t("ai.externalMcp.http.warning")}</p>
|
|
) : null}
|
|
<p className="text-xs text-muted-foreground leading-5">{t("ai.externalMcp.http.wslHint")}</p>
|
|
{settings.enabled && !externalMcpEnabled ? (
|
|
<p className="text-xs text-amber-500">{t("ai.externalMcp.http.requiresExternalMcp")}</p>
|
|
) : null}
|
|
{errorText ? <p className="text-xs text-destructive">{errorText}</p> : null}
|
|
<div className="space-y-2.5 rounded-md border border-border/60 bg-background/50 p-3">
|
|
<CopyableCodeBlock
|
|
label={t("ai.externalMcp.http.url")}
|
|
value={url}
|
|
copyKey="http-url"
|
|
copied={copied}
|
|
onCopy={onCopy}
|
|
copyLabel={t("ai.externalMcp.copy")}
|
|
copiedLabel={t("ai.externalMcp.copied")}
|
|
/>
|
|
<CopyableCodeBlock
|
|
label={t("ai.externalMcp.http.token")}
|
|
value={token}
|
|
copyKey="http-token"
|
|
copied={copied}
|
|
onCopy={onCopy}
|
|
copyLabel={t("ai.externalMcp.copy")}
|
|
copiedLabel={t("ai.externalMcp.copied")}
|
|
emptyLabel={t("ai.externalMcp.unavailable")}
|
|
/>
|
|
<div className="flex items-center justify-between gap-3">
|
|
<p className="text-xs text-muted-foreground leading-5">{t("ai.externalMcp.http.regenerateHint")}</p>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
size="sm"
|
|
disabled={!externalMcpEnabled || !settings.enabled || rotating}
|
|
onClick={onRotateToken}
|
|
>
|
|
{t("ai.externalMcp.http.regenerate")}
|
|
</Button>
|
|
</div>
|
|
<CopyableCodeBlock
|
|
label={t("ai.externalMcp.http.codexSnippet")}
|
|
value={buildCodexHttpToml(url)}
|
|
copyKey="http-codex"
|
|
copied={copied}
|
|
onCopy={onCopy}
|
|
copyLabel={t("ai.externalMcp.copy")}
|
|
copiedLabel={t("ai.externalMcp.copied")}
|
|
/>
|
|
<CopyableCodeBlock
|
|
label={t("ai.externalMcp.http.envLine")}
|
|
value={token ? buildCodexHttpEnvLine(token) : ""}
|
|
copyKey="http-env"
|
|
copied={copied}
|
|
onCopy={onCopy}
|
|
copyLabel={t("ai.externalMcp.copy")}
|
|
copiedLabel={t("ai.externalMcp.copied")}
|
|
emptyLabel={t("ai.externalMcp.unavailable")}
|
|
/>
|
|
<CopyableCodeBlock
|
|
label={t("ai.externalMcp.http.claudeCommand")}
|
|
value={token ? buildClaudeHttpAddCommand(url, token) : ""}
|
|
copyKey="http-claude"
|
|
copied={copied}
|
|
onCopy={onCopy}
|
|
copyLabel={t("ai.externalMcp.copy")}
|
|
copiedLabel={t("ai.externalMcp.copied")}
|
|
emptyLabel={t("ai.externalMcp.unavailable")}
|
|
/>
|
|
<CopyableCodeBlock
|
|
label={t("ai.externalMcp.http.genericSnippet")}
|
|
value={token ? buildGenericHttpMcpJson(url, token) : ""}
|
|
copyKey="http-json"
|
|
copied={copied}
|
|
onCopy={onCopy}
|
|
copyLabel={t("ai.externalMcp.copy")}
|
|
copiedLabel={t("ai.externalMcp.copied")}
|
|
emptyLabel={t("ai.externalMcp.unavailable")}
|
|
/>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|