whoami
Check which npm user is currently authenticated. If not logged in, configure the NPM_TOKEN environment variable in your MCP settings to enable authentication.
Instructions
Check which npm user is currently authenticated. If not logged in, set NPM_TOKEN env var in MCP config.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/index.ts:280-299 (handler)Primary handler for the 'whoami' tool. Registers via server.tool() with no parameters (empty schema {}). Runs `npm whoami` via run() helper, returns stdout.trim() on success, or an error message with hints about NPM_TOKEN on failure.
// ── npm whoami ── server.tool( "whoami", "Check which npm user is currently authenticated. If not logged in, set NPM_TOKEN env var in MCP config.", {}, async () => { try { const { stdout } = await run(["whoami"]); return { content: [{ type: "text", text: stdout.trim() }] }; } catch (e: any) { const hint = NPM_TOKEN ? "Token is set but invalid." : "No NPM_TOKEN set. Add it to your MCP server config env, or run `npm login` first."; return { content: [{ type: "text", text: `Not logged in. ${hint}\n${e.stderr || e.message}` }], isError: true, }; } }, ); - src/index.ts:281-299 (registration)Registration of the 'whoami' tool via server.tool() in the main MCP server. The tool has no input parameters (empty Zod schema {}).
server.tool( "whoami", "Check which npm user is currently authenticated. If not logged in, set NPM_TOKEN env var in MCP config.", {}, async () => { try { const { stdout } = await run(["whoami"]); return { content: [{ type: "text", text: stdout.trim() }] }; } catch (e: any) { const hint = NPM_TOKEN ? "Token is set but invalid." : "No NPM_TOKEN set. Add it to your MCP server config env, or run `npm login` first."; return { content: [{ type: "text", text: `Not logged in. ${hint}\n${e.stderr || e.message}` }], isError: true, }; } }, ); - src/index.ts:1257-1257 (registration)Sandbox registration of the 'whoami' tool in createSandboxServer(). Uses a noop handler that returns 'sandbox' placeholder text.
sandbox.tool("whoami", "Check which npm user is currently authenticated", {}, noop); - src/index.ts:284-284 (schema)Schema for the 'whoami' tool: empty object {} since the tool takes no parameters.
{}, - src/index.ts:26-38 (helper)The `run()` helper function that executes npm commands. Used by the whoami handler to run `npm whoami`. It appends npmrcArgs (for NPM_TOKEN-based auth) and sets timeout/maxBuffer.
async function run( args: string[], cwd?: string, ): Promise<{ stdout: string; stderr: string }> { const fullArgs = [...args, ...npmrcArgs]; const opts: { cwd?: string; timeout: number; env: NodeJS.ProcessEnv; maxBuffer: number } = { timeout: 120_000, maxBuffer: 10 * 1024 * 1024, // 10MB buffer for large outputs env: { ...process.env, NO_COLOR: "1" }, }; if (cwd) opts.cwd = cwd; return exec(NPM, fullArgs, opts); }