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

Function traceCloudMcpRequest

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

Source from the content-addressed store, hash-verified

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

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