MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / startBedrockMockServer

Function startBedrockMockServer

apps/vscode-e2e/src/bedrock-mock-server.ts:91–152  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

89}
90
91export async function startBedrockMockServer(): Promise<BedrockMockServer> {
92 // HTTP/2 cleartext (h2c) — matches what @aws-sdk/client-bedrock-runtime uses by default.
93 const server = http2.createServer()
94 let lastRequestHeaders: http2.IncomingHttpHeaders | undefined
95 const sessions = new Set<http2.ServerHttp2Session>()
96
97 server.on("session", (session) => {
98 sessions.add(session)
99 session.on("close", () => sessions.delete(session))
100 })
101
102 server.on("stream", (stream, headers) => {
103 const path = headers[":path"] as string
104 const method = headers[":method"] as string
105
106 if (!path?.includes("converse-stream") || method !== "POST") {
107 stream.respond({ ":status": 404 })
108 stream.end(JSON.stringify({ error: { message: "Not found", type: "not_found" } }))
109 return
110 }
111
112 lastRequestHeaders = headers
113
114 // Drain the request body before responding (AWS SDK sends the full request before reading).
115 stream.resume()
116 stream.on("end", () => {
117 stream.respond({
118 ":status": 200,
119 "content-type": "application/vnd.amazon.eventstream",
120 })
121 const frames = buildToolCallFrames(
122 "attempt_completion",
123 "tooluse_bedrock_mock_001",
124 JSON.stringify({ result: "4" }),
125 )
126 for (const frame of frames) {
127 stream.write(frame)
128 }
129 stream.end()
130 })
131 })
132
133 await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve))
134 const addr = server.address() as net.AddressInfo
135 const url = `http://127.0.0.1:${addr.port}`
136
137 return {
138 url,
139 get lastRequestHeaders() {
140 return lastRequestHeaders
141 },
142 close: () => {
143 // Destroy all open HTTP/2 sessions first so server.close() resolves immediately
144 // instead of waiting for the extension's retry loop to exhaust itself.
145 for (const session of sessions) {
146 session.destroy()
147 }
148 sessions.clear()

Callers 1

bedrock.test.tsFile · 0.90

Calls 9

buildToolCallFramesFunction · 0.85
respondMethod · 0.80
resumeMethod · 0.80
onMethod · 0.65
deleteMethod · 0.65
writeMethod · 0.65
listenMethod · 0.65
clearMethod · 0.65
closeMethod · 0.65

Tested by

no test coverage detected