docs: document gateway mcp helpers · openclaw/openclaw@1e43873
steipete
·
2026-06-05
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +// OpenAI-compatible embeddings HTTP endpoint. |
| 2 | +// Bridges /v1/embeddings requests to configured OpenClaw memory providers. |
1 | 3 | import { Buffer } from "node:buffer"; |
2 | 4 | import type { IncomingMessage, ServerResponse } from "node:http"; |
3 | 5 | import { |
@@ -80,6 +82,7 @@ function resolveInputTexts(input: unknown): string[] | null {
|
80 | 82 | } |
81 | 83 | |
82 | 84 | function encodeEmbeddingBase64(embedding: number[]): string { |
| 85 | +// OpenAI-compatible base64 embeddings are raw float32 bytes, not JSON. |
83 | 86 | const float32 = Float32Array.from(embedding); |
84 | 87 | return Buffer.from(float32.buffer).toString("base64"); |
85 | 88 | } |
|
| Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +// MCP loopback HTTP request helpers. |
| 2 | +// Authenticates local MCP POST requests and extracts scoped Gateway context. |
1 | 3 | import type { IncomingMessage, ServerResponse } from "node:http"; |
2 | 4 | import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce"; |
3 | 5 | import type { SourceReplyDeliveryMode } from "../auto-reply/get-reply-options.types.js"; |
@@ -176,6 +178,8 @@ export async function readMcpHttpBody(req: IncomingMessage): Promise<string> {
|
176 | 178 | const chunks: Buffer[] = []; |
177 | 179 | let received = 0; |
178 | 180 | let settled = false; |
| 181 | +// Remove listeners on every terminal path; oversized bodies keep the error |
| 182 | +// listener briefly so Node can deliver the pause/error safely. |
179 | 183 | const cleanup = (options?: { keepErrorListener?: boolean }) => { |
180 | 184 | req.off("data", onData); |
181 | 185 | req.off("end", onEnd); |
|
| Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +// MCP loopback runtime scope cache. |
| 2 | +// Resolves Gateway-visible tools for MCP clients with short-lived schema caching. |
1 | 3 | import type { SourceReplyDeliveryMode } from "../auto-reply/get-reply-options.types.js"; |
2 | 4 | import type { InboundEventKind } from "../channels/inbound-event/kind.js"; |
3 | 5 | import type { OpenClawConfig } from "../config/types.openclaw.js"; |
|
| Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +// MCP loopback tool schema projection. |
| 2 | +// Converts gateway-scoped tools into MCP tools/list-compatible schemas. |
1 | 3 | import { isRecord } from "@openclaw/normalization-core/record-coerce"; |
2 | 4 | import { uniqueValues } from "@openclaw/normalization-core/string-normalization"; |
3 | 5 | import { logWarn } from "../logger.js"; |
@@ -56,6 +58,8 @@ function readLoopbackToolParameters(tool: McpLoopbackTool): Record<string, unkno
|
56 | 58 | } |
57 | 59 | |
58 | 60 | function flattenUnionSchema(raw: Record<string, unknown>): Record<string, unknown> { |
| 61 | +// MCP clients vary in union-schema support. Merge only safe object variants |
| 62 | +// and keep common required fields so generated forms remain usable. |
59 | 63 | const variants = (raw.anyOf ?? raw.oneOf) as unknown[] | undefined; |
60 | 64 | if (!Array.isArray(variants) || variants.length === 0) { |
61 | 65 | return raw; |
|
| Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +// MCP loopback HTTP server. |
| 2 | +// Exposes Gateway-scoped tools to local MCP clients over bearer-auth loopback. |
1 | 3 | import crypto from "node:crypto"; |
2 | 4 | import { |
3 | 5 | createServer as createHttpServer, |
@@ -207,6 +209,8 @@ export async function startMcpLoopbackServer(port = 0): Promise<{
|
207 | 209 | if (!address || typeof address === "string") { |
208 | 210 | throw new Error("mcp loopback did not bind to a TCP port"); |
209 | 211 | } |
| 212 | +// Register tokens only after the TCP listener is live so clients never learn |
| 213 | +// a bearer token for a server that failed to bind. |
210 | 214 | setActiveMcpLoopbackRuntime({ port: address.port, ownerToken, nonOwnerToken }); |
211 | 215 | logDebug(`mcp loopback listening on 127.0.0.1:${address.port}`); |
212 | 216 | |
|
| Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +// OpenResponses file-content boundary helper. |
| 2 | +// Marks uploaded/read file text as untrusted external model input. |
1 | 3 | import { wrapExternalContent } from "../security/external-content.js"; |
2 | 4 | |
3 | 5 | // OpenResponses file content is untrusted model input. The wrapper preserves |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。