Skip to content

Commit af8b68d

Browse files
committed
Enhance tool call return values
1 parent 55984f8 commit af8b68d

File tree

4 files changed

+35
-28
lines changed

4 files changed

+35
-28
lines changed

.vscode/settings.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"cSpell.words": [
3+
"devcontainer",
4+
"devcontainers",
5+
"streamable",
6+
"streamablehttp",
7+
"resumability",
8+
"onsessioninitialized",
9+
"modelcontextprotocol"
10+
]
11+
}

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
"prepare": "npm run build",
1919
"watch": "tsc --watch",
2020
"start": "node dist/index.js",
21-
"start:sse": "node dist/sse.js",
22-
"start:http": "node dist/streamableHttp.js",
2321
"lint": "eslint -f stylish src",
2422
"lint:fix": "eslint -f stylish --fix src"
2523
},

src/devcontainer.ts

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,8 @@ import path from "path";
44
import { createRequire } from "module";
55

66
export const NULL_DEVICE = '/dev/null';
7-
const require = createRequire(import.meta.url);
8-
9-
type CommandResult = Promise<{code: number, stdout: string}>;
107

11-
function devcontainerBinaryPath(): string {
12-
try {
13-
const pkgPath = require.resolve('@devcontainers/cli/package.json');
14-
const pkg = require(pkgPath);
15-
return path.join(path.dirname(pkgPath), pkg.bin.devcontainer);
16-
} catch (error) {
17-
throw new Error('Failed to locate devcontainer CLI: ' + (error as Error).message);
18-
}
19-
}
8+
type CommandResult = Promise<string>;
209

2110
interface DevcontainerOptions {
2211
stdioFilePath?: string;
@@ -35,6 +24,18 @@ interface DevContainerExecOptions extends DevcontainerOptions {
3524
command: string[];
3625
}
3726

27+
const require = createRequire(import.meta.url);
28+
29+
function devcontainerBinaryPath(): string {
30+
try {
31+
const pkgPath = require.resolve('@devcontainers/cli/package.json');
32+
const pkg = require(pkgPath);
33+
return path.join(path.dirname(pkgPath), pkg.bin.devcontainer);
34+
} catch (error) {
35+
throw new Error('Failed to locate devcontainer CLI: ' + (error as Error).message);
36+
}
37+
}
38+
3839
function createOutputStream(stdioFilePath: string = NULL_DEVICE): fs.WriteStream {
3940
try {
4041
return fs.createWriteStream(stdioFilePath, { flags: 'w' })
@@ -50,8 +51,8 @@ async function runCommand(args: string[], stdoutStream: fs.WriteStream): Command
5051
stdio: ['ignore', 'pipe', 'pipe'],
5152
} as SpawnOptions);
5253

53-
let stdoutData = '';
54-
let stderrData = '';
54+
const stdoutData: string[] = [];
55+
const stderrData: string[] = [];
5556

5657
child.on('error', (error) => {
5758
cleanup(error);
@@ -60,14 +61,13 @@ async function runCommand(args: string[], stdoutStream: fs.WriteStream): Command
6061

6162
// Pipe stdout to the stream as before, but also collect it
6263
child.stdout?.on('data', (data) => {
63-
console.log(data.toString())
64-
stdoutData += JSON.stringify(data.toString(), null, 2);
64+
stdoutData.push(data.toString())
6565
stdoutStream.write(data);
6666
});
6767

6868
// Collect stderr data instead of piping to process.stderr
6969
child.stderr?.on('data', (data) => {
70-
stderrData += data.toString();
70+
stderrData.push(data.toString().trim());
7171
});
7272

7373
const cleanup = (error?: Error) => {
@@ -84,17 +84,14 @@ async function runCommand(args: string[], stdoutStream: fs.WriteStream): Command
8484
child.on('close', (code, signal) => {
8585
cleanup();
8686
if (code === 0) {
87-
resolve({
88-
code: code,
89-
stdout: stdoutData
90-
});
87+
resolve(`success with code ${code}\n-------\n${stdoutData.join('\n\n')}`);
9188
} else {
9289
const reason = signal
9390
? `terminated by signal ${signal}`
9491
: `exited with code ${code}`;
9592

9693
// Combine the error message with the collected stderr output
97-
const errorMessage = `Command failed: devcontainer ${args.join(' ')} (${reason})\n${stderrData.trim()}`;
94+
const errorMessage = `Command failed: devcontainer ${args.join(' ')} (${reason})\n-------\n${stderrData.join('\n\n')}`;
9895
reject(new Error(errorMessage));
9996
}
10097
});
@@ -119,4 +116,4 @@ export async function devExec(options: DevContainerExecOptions): CommandResult {
119116
options.workspaceFolder,
120117
...options.command
121118
], stream);
122-
}
119+
}

src/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const scriptName = args[0] || 'stdio';
77
async function run() {
88
try {
99
// Dynamically import only the requested module to prevent all modules from initializing
10-
switch (scriptName) {
10+
switch (scriptName.toLowerCase()) {
1111
case 'stdio':
1212
// Import and run the default server
1313
await import('./stdio.js');
@@ -16,7 +16,8 @@ async function run() {
1616
// Import and run the SSE server
1717
await import('./sse.js');
1818
break;
19-
case 'streamableHttp':
19+
case 'http':
20+
case 'streamablehttp':
2021
// Import and run the streamable HTTP server
2122
await import('./streamableHttp.js');
2223
break;
@@ -34,4 +35,4 @@ async function run() {
3435
}
3536
}
3637

37-
run();
38+
run();

0 commit comments

Comments
 (0)