cleanup mcp server too

This commit is contained in:
Mateusz Burzyński
2026-01-08 11:21:23 +01:00
parent 2bd12b9553
commit 317ebd3431
3 changed files with 125 additions and 131 deletions
+23 -21
View File
@@ -126416,7 +126416,7 @@ async function startMcpHttpServer(ctx) {
const url2 = `http://${host}:${port}${endpoint2}`; const url2 = `http://${host}:${port}${endpoint2}`;
return { return {
url: url2, url: url2,
close: async () => { [Symbol.asyncDispose]: async () => {
await server.stop(); await server.stop();
} }
}; };
@@ -126556,18 +126556,19 @@ var Inputs = type({
...keyInputDefs ...keyInputDefs
}); });
async function main(inputs) { async function main(inputs) {
var _stack = []; var _stack2 = [];
try { try {
const timer = new Timer(); const timer = new Timer();
const installationToken = __using(_stack, await setupGitHubInstallationToken(), true); const tokenRef = __using(_stack2, await setupGitHubInstallationToken(), true);
let mcpServerClose;
let payload; let payload;
try {
var _stack = [];
try { try {
payload = parsePayload(inputs); payload = parsePayload(inputs);
Inputs.assert(inputs); Inputs.assert(inputs);
setupGitConfig(); setupGitConfig();
const [githubSetup, sharedTempDir] = await Promise.all([ const [githubSetup, sharedTempDir] = await Promise.all([
initializeGitHub(installationToken.token), initializeGitHub(tokenRef.token),
createTempDirectory() createTempDirectory()
]); ]);
timer.checkpoint("githubSetup"); timer.checkpoint("githubSetup");
@@ -126589,9 +126590,9 @@ async function main(inputs) {
} }
const toolState = {}; const toolState = {};
const [cliPath] = await Promise.all([ const [cliPath] = await Promise.all([
installAgentCli({ agent: agent2, token: installationToken.token }), installAgentCli({ agent: agent2, token: tokenRef.token }),
setupGitAuth({ setupGitAuth({
token: installationToken.token, token: tokenRef.token,
owner: githubSetup.owner, owner: githubSetup.owner,
name: githubSetup.name, name: githubSetup.name,
payload: resolvedPayload, payload: resolvedPayload,
@@ -126631,7 +126632,7 @@ async function main(inputs) {
const toolContext = { const toolContext = {
owner: githubSetup.owner, owner: githubSetup.owner,
name: githubSetup.name, name: githubSetup.name,
githubInstallationToken: installationToken.token, githubInstallationToken: tokenRef.token,
octokit: githubSetup.octokit, octokit: githubSetup.octokit,
payload: resolvedPayload, payload: resolvedPayload,
repo: githubSetup.repo, repo: githubSetup.repo,
@@ -126643,17 +126644,15 @@ async function main(inputs) {
runId, runId,
jobId jobId
}; };
const { url: mcpServerUrl, close: mcpServerCloseFunc } = await startMcpHttpServer(toolContext); const mcpHttpServer = __using(_stack, await startMcpHttpServer(toolContext), true);
mcpServerClose = mcpServerCloseFunc; log.info(`\u{1F680} MCP server started at ${mcpHttpServer.url}`);
log.info(`\u{1F680} MCP server started at ${mcpServerUrl}`); const mcpServers = createMcpConfigs(mcpHttpServer.url);
const mcpServers = createMcpConfigs(mcpServerUrl);
log.debug(`\u{1F4CB} MCP Config: ${JSON.stringify(mcpServers, null, 2)}`); log.debug(`\u{1F4CB} MCP Config: ${JSON.stringify(mcpServers, null, 2)}`);
timer.checkpoint("mcpServer"); timer.checkpoint("mcpServer");
const ctx = { const ctx = {
...toolContext, ...toolContext,
inputs, inputs,
mcpServerUrl, mcpServerUrl: mcpHttpServer.url,
mcpServerClose: mcpServerCloseFunc,
mcpServers, mcpServers,
cliPath, cliPath,
apiKey: apiKeySetup.apiKey, apiKey: apiKeySetup.apiKey,
@@ -126670,6 +126669,12 @@ To use "Fix \u{1F44D}s", add a \u{1F44D} reaction to one or more inline review c
const result = await runAgent(ctx); const result = await runAgent(ctx);
const mainResult = await handleAgentResult(result); const mainResult = await handleAgentResult(result);
return mainResult; return mainResult;
} catch (_) {
var _error = _, _hasError = true;
} finally {
var _promise2 = __callDispose(_stack, _error, _hasError);
_promise2 && await _promise2;
}
} catch (error41) { } catch (error41) {
const errorMessage = error41 instanceof Error ? error41.message : "Unknown error occurred"; const errorMessage = error41 instanceof Error ? error41.message : "Unknown error occurred";
log.error(errorMessage); log.error(errorMessage);
@@ -126687,15 +126692,12 @@ To use "Fix \u{1F44D}s", add a \u{1F44D} reaction to one or more inline review c
await ensureProgressCommentUpdated(payload); await ensureProgressCommentUpdated(payload);
} catch { } catch {
} }
if (mcpServerClose) {
await mcpServerClose();
} }
} } catch (_2) {
} catch (_) { var _error2 = _2, _hasError2 = true;
var _error = _, _hasError = true;
} finally { } finally {
var _promise2 = __callDispose(_stack, _error, _hasError); var _promise3 = __callDispose(_stack2, _error2, _hasError2);
_promise2 && await _promise2; _promise3 && await _promise3;
} }
} }
function agentHasApiKeys(agent2, inputs) { function agentHasApiKeys(agent2, inputs) {
+9 -17
View File
@@ -66,8 +66,7 @@ type ApiKeySetup =
export async function main(inputs: Inputs): Promise<MainResult> { export async function main(inputs: Inputs): Promise<MainResult> {
const timer = new Timer(); const timer = new Timer();
await using installationToken = await setupGitHubInstallationToken(); await using tokenRef = await setupGitHubInstallationToken();
let mcpServerClose: (() => Promise<void>) | undefined;
let payload: Payload | undefined; let payload: Payload | undefined;
try { try {
@@ -78,7 +77,7 @@ export async function main(inputs: Inputs): Promise<MainResult> {
// phase 2: fast setup (github + temp dir) // phase 2: fast setup (github + temp dir)
const [githubSetup, sharedTempDir] = await Promise.all([ const [githubSetup, sharedTempDir] = await Promise.all([
initializeGitHub(installationToken.token), initializeGitHub(tokenRef.token),
createTempDirectory(), createTempDirectory(),
]); ]);
timer.checkpoint("githubSetup"); timer.checkpoint("githubSetup");
@@ -106,9 +105,9 @@ export async function main(inputs: Inputs): Promise<MainResult> {
// phase 5: parallel long-running operations (agent install + git auth) // phase 5: parallel long-running operations (agent install + git auth)
const toolState: ToolState = {}; const toolState: ToolState = {};
const [cliPath] = await Promise.all([ const [cliPath] = await Promise.all([
installAgentCli({ agent, token: installationToken.token }), installAgentCli({ agent, token: tokenRef.token }),
setupGitAuth({ setupGitAuth({
token: installationToken.token, token: tokenRef.token,
owner: githubSetup.owner, owner: githubSetup.owner,
name: githubSetup.name, name: githubSetup.name,
payload: resolvedPayload, payload: resolvedPayload,
@@ -155,7 +154,7 @@ export async function main(inputs: Inputs): Promise<MainResult> {
const toolContext: ToolContext = { const toolContext: ToolContext = {
owner: githubSetup.owner, owner: githubSetup.owner,
name: githubSetup.name, name: githubSetup.name,
githubInstallationToken: installationToken.token, githubInstallationToken: tokenRef.token,
octokit: githubSetup.octokit, octokit: githubSetup.octokit,
payload: resolvedPayload, payload: resolvedPayload,
repo: githubSetup.repo, repo: githubSetup.repo,
@@ -168,11 +167,10 @@ export async function main(inputs: Inputs): Promise<MainResult> {
jobId, jobId,
}; };
const { url: mcpServerUrl, close: mcpServerCloseFunc } = await startMcpHttpServer(toolContext); await using mcpHttpServer = await startMcpHttpServer(toolContext);
mcpServerClose = mcpServerCloseFunc; log.info(`🚀 MCP server started at ${mcpHttpServer.url}`);
log.info(`🚀 MCP server started at ${mcpServerUrl}`);
const mcpServers = createMcpConfigs(mcpServerUrl); const mcpServers = createMcpConfigs(mcpHttpServer.url);
log.debug(`📋 MCP Config: ${JSON.stringify(mcpServers, null, 2)}`); log.debug(`📋 MCP Config: ${JSON.stringify(mcpServers, null, 2)}`);
timer.checkpoint("mcpServer"); timer.checkpoint("mcpServer");
@@ -180,8 +178,7 @@ export async function main(inputs: Inputs): Promise<MainResult> {
const ctx: AgentContext = { const ctx: AgentContext = {
...toolContext, ...toolContext,
inputs, inputs,
mcpServerUrl, mcpServerUrl: mcpHttpServer.url,
mcpServerClose: mcpServerCloseFunc,
mcpServers, mcpServers,
cliPath, cliPath,
apiKey: apiKeySetup.apiKey, apiKey: apiKeySetup.apiKey,
@@ -224,10 +221,6 @@ export async function main(inputs: Inputs): Promise<MainResult> {
} catch { } catch {
// error updating comment, but don't let it mask the original error // error updating comment, but don't let it mask the original error
} }
if (mcpServerClose) {
await mcpServerClose();
}
} }
} }
@@ -319,7 +312,6 @@ export interface ToolContext {
export interface AgentContext extends Readonly<ToolContext> { export interface AgentContext extends Readonly<ToolContext> {
readonly inputs: Inputs; readonly inputs: Inputs;
readonly mcpServerUrl: string; readonly mcpServerUrl: string;
readonly mcpServerClose: () => Promise<void>;
readonly mcpServers: ReturnType<typeof createMcpConfigs>; readonly mcpServers: ReturnType<typeof createMcpConfigs>;
readonly cliPath: string; readonly cliPath: string;
readonly apiKey: string; readonly apiKey: string;
+2 -2
View File
@@ -64,7 +64,7 @@ async function findAvailablePort(startPort: number): Promise<number> {
*/ */
export async function startMcpHttpServer( export async function startMcpHttpServer(
ctx: ToolContext ctx: ToolContext
): Promise<{ url: string; close: () => Promise<void> }> { ): Promise<{ url: string; [Symbol.asyncDispose]: () => Promise<void> }> {
const server = new FastMCP({ const server = new FastMCP({
name: ghPullfrogMcpName, name: ghPullfrogMcpName,
version: "0.0.1", version: "0.0.1",
@@ -119,7 +119,7 @@ export async function startMcpHttpServer(
return { return {
url, url,
close: async () => { [Symbol.asyncDispose]: async () => {
await server.stop(); await server.stop();
}, },
}; };