Skip to content

Commit bf1db29

Browse files
committed
feat: refactor dev.js for improved port handling, enhance error reporting in waitForServerReady, and restructure exercises for worker functionality with new tools and resources
1 parent 6c45477 commit bf1db29

File tree

11 files changed

+49
-17
lines changed

11 files changed

+49
-17
lines changed

epicshop/mcp-dev/dev.js

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ const { createProxyServer } = httpProxy
1313
const [, , ...args] = process.argv
1414
const [transport = 'streamable-http'] = args
1515

16-
const proxyPort = process.env.PORT || 3000
16+
const proxyPort = Number(process.env.PORT || 3000)
1717
const inspectorServerPort = await getPort({
18-
port: Array.from({ length: 1000 }, (_, i) => i + 10000),
18+
port: Array.from({ length: 100 }, (_, i) => proxyPort + i + 50000),
1919
exclude: [process.env.PORT].filter(Boolean).map(Number),
2020
})
2121
const inspectorClientPort = await getPort({
22-
port: Array.from({ length: 1000 }, (_, i) => i + 9000),
22+
port: Array.from({ length: 100 }, (_, i) => proxyPort + i + 51000),
2323
exclude: [process.env.PORT, inspectorServerPort].filter(Boolean).map(Number),
2424
})
2525
const mcpServerPort = await getPort({
26-
port: Array.from({ length: 1000 }, (_, i) => i + 11000),
26+
port: Array.from({ length: 100 }, (_, i) => proxyPort + i + 52000),
2727
exclude: [process.env.PORT, inspectorServerPort, inspectorClientPort]
2828
.filter(Boolean)
2929
.map(Number),
@@ -112,9 +112,28 @@ async function waitForServerReady({ process: childProcess, textMatch, name }) {
112112
if (!childProcess) return
113113

114114
return new Promise((resolve, reject) => {
115+
const outputBuffer = []
116+
117+
function addToBuffer(channel, data) {
118+
outputBuffer.push({ channel, data })
119+
}
120+
121+
function printAndReject(reason) {
122+
// Print all buffered output in sequence
123+
for (const { channel, data } of outputBuffer) {
124+
const str = data.toString()
125+
if (channel === 'stdout') {
126+
process.stdout.write(styleText('blue', `${name} `) + str)
127+
} else if (channel === 'stderr') {
128+
process.stderr.write(styleText('red', `${name} `) + str)
129+
}
130+
}
131+
reject(reason)
132+
}
133+
115134
const timeout = setTimeout(() => {
116135
process.kill()
117-
reject(new Error(`${name} failed to start within 10 seconds`))
136+
printAndReject(new Error(`${name} failed to start within 10 seconds`))
118137
}, 10_000)
119138

120139
function searchForMatch(data) {
@@ -124,21 +143,34 @@ async function waitForServerReady({ process: childProcess, textMatch, name }) {
124143
// Remove the listeners after finding the match
125144
childProcess.stdout.removeListener('data', searchForMatch)
126145
childProcess.stderr.removeListener('data', searchForMatch)
146+
childProcess.stdout.removeListener('data', bufferStdout)
147+
childProcess.stderr.removeListener('data', bufferStderr)
127148
resolve()
128149
}
129150
}
130-
childProcess.stdout.on('data', searchForMatch)
131-
childProcess.stderr.on('data', searchForMatch)
151+
152+
function bufferStdout(data) {
153+
addToBuffer('stdout', data)
154+
searchForMatch(data)
155+
}
156+
157+
function bufferStderr(data) {
158+
addToBuffer('stderr', data)
159+
searchForMatch(data)
160+
}
161+
162+
childProcess.stdout.on('data', bufferStdout)
163+
childProcess.stderr.on('data', bufferStderr)
132164

133165
childProcess.on('error', (err) => {
134166
clearTimeout(timeout)
135-
reject(err)
167+
printAndReject(err)
136168
})
137169

138170
childProcess.on('exit', (code) => {
139171
if (code !== 0) {
140172
clearTimeout(timeout)
141-
reject(new Error(`${name} exited with code ${code}`))
173+
printAndReject(new Error(`${name} exited with code ${code}`))
142174
}
143175
})
144176
})
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# Final
1+
# Start

exercises/01.start/01.problem/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"extends": ["@epic-web/config/typescript"],
33
"include": [
44
"types/**/*.d.ts",
5-
"src/**/*.ts",
5+
"worker/**/*.ts",
66
"test/**/*.ts",
77
"vitest.config.ts"
88
]

exercises/01.start/01.problem/src/client.ts renamed to exercises/01.start/01.problem/worker/client.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@ import { DBClient } from '@epic-web/epicme-db-client'
22

33
const EPIC_ME_SERVER_URL = 'http://localhost:7787'
44

5-
export function getClient() {
6-
return new DBClient(EPIC_ME_SERVER_URL)
7-
}
5+
export const db = new DBClient(EPIC_ME_SERVER_URL)

exercises/01.start/01.problem/src/index.ts renamed to exercises/01.start/01.problem/worker/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
type LoggingLevel,
66
} from '@modelcontextprotocol/sdk/types.js'
77
import { McpAgent } from 'agents/mcp'
8-
import { getClient } from './client.ts'
8+
import { db } from './client.ts'
99
import { initializePrompts } from './prompts.ts'
1010
import { initializeResources } from './resources.ts'
1111
import { initializeTools } from './tools.ts'
@@ -39,7 +39,7 @@ You can also help users add tags to their entries and get all tags for an entry.
3939
)
4040

4141
async init() {
42-
this.db = getClient()
42+
this.db = db
4343
this.server.server.setRequestHandler(
4444
SetLevelRequestSchema,
4545
async (request) => {

exercises/01.start/01.problem/wrangler.jsonc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{
66
"$schema": "../../../node_modules/wrangler/config-schema.json",
77
"name": "epic-me-mcp",
8-
"main": "src/index.ts",
8+
"main": "worker/index.ts",
99
"compatibility_date": "2025-04-17",
1010
"compatibility_flags": ["nodejs_compat"],
1111
"migrations": [

0 commit comments

Comments
 (0)