MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / traceCloudMcpRequest

Function traceCloudMcpRequest

apps/cloud/src/server.ts:111–162  ·  view source on GitHub ↗
(
  request: Request,
  _env: Env,
  ctx: ExecutionContext,
  handle: (tracedRequest: Request) => Promise<Response>,
)

Source from the content-addressed store, hash-verified

109};
110
111const traceCloudMcpRequest = async (
112 request: Request,
113 _env: Env,
114 ctx: ExecutionContext,
115 handle: (tracedRequest: Request) => Promise<Response>,
116): Promise<Response> => {
117 if (!installTracerProvider()) return handle(request);
118
119 const url = new URL(request.url);
120 const inbound = parseTraceparent(request.headers.get("traceparent"), null);
121 const parentContext = inbound
122 ? trace.setSpanContext(context.active(), {
123 traceId: inbound.traceId,
124 spanId: inbound.spanId,
125 traceFlags: inbound.traceFlags,
126 isRemote: true,
127 })
128 : context.active();
129
130 return tracer.startActiveSpan(
131 `http.server ${request.method}`,
132 { kind: SpanKind.SERVER },
133 parentContext,
134 async (span) => {
135 span.setAttribute(ATTR_HTTP_REQUEST_METHOD, request.method);
136 span.setAttribute(ATTR_URL_FULL, request.url);
137 span.setAttribute(ATTR_URL_PATH, url.pathname);
138 span.setAttribute(ATTR_URL_SCHEME, url.protocol.replace(/:$/, ""));
139 // oxlint-disable-next-line executor/no-try-catch-or-throw -- adapter boundary; observe response/error for span status, keep trace export alive after the Agents bridge resolves or rejects
140 try {
141 const response = await handle(withTraceparent(request, span.spanContext()));
142 span.setAttribute(ATTR_HTTP_RESPONSE_STATUS_CODE, response.status);
143 if (response.status >= 500) {
144 span.setStatus({ code: SpanStatusCode.ERROR, message: `HTTP ${response.status}` });
145 }
146 return response;
147 } catch (err) {
148 // oxlint-disable-next-line executor/no-instanceof-error, executor/no-unknown-error-message -- adapter boundary: Cloudflare's fetch callback throws untyped; normalized only for the OTel span record, the original error is rethrown below
149 const cause = err instanceof Error ? err : String(err);
150 span.recordException(cause);
151 // oxlint-disable-next-line executor/no-unknown-error-message -- adapter boundary: same normalization as the recordException line above
152 const message = typeof cause === "string" ? cause : cause.message;
153 span.setStatus({ code: SpanStatusCode.ERROR, message });
154 // oxlint-disable-next-line executor/no-try-catch-or-throw -- adapter boundary; preserve original error to Cloudflare runtime
155 throw err;
156 } finally {
157 span.end();
158 ctx.waitUntil(flushTracerProvider());
159 }
160 },
161 );
162};
163
164const mcpAgentHandler = makeCloudMcpAgentHandler();
165

Callers 1

server.tsFile · 0.85

Calls 10

installTracerProviderFunction · 0.90
parseTraceparentFunction · 0.90
flushTracerProviderFunction · 0.90
withTraceparentFunction · 0.85
setAttributeMethod · 0.80
endMethod · 0.80
getMethod · 0.65
replaceMethod · 0.65
waitUntilMethod · 0.65
handleFunction · 0.50

Tested by

no test coverage detected