Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions server.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"url": "https://github.com/atriumn/tokencost-dev",
"source": "github"
},
"version": "0.1.2",
"version": "0.1.3",
"packages": [
{
"registryType": "npm",
"identifier": "tokencost-dev",
"version": "0.1.2",
"version": "0.1.3",
"transport": {
"type": "stdio"
}
Expand Down
61 changes: 40 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,39 @@ import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprot
import { getModels } from "./pricing.js";
import { executeTool, tools } from "./tools.js";

const server = new Server(
{
name: "tokencost-dev",
version: "0.1.0",
},
{
capabilities: {
tools: {},
function createServer(): Server {
const server = new Server(
{
name: "tokencost-dev",
version: "0.1.0",
},
},
);
{
capabilities: {
tools: {},
},
},
);

server.setRequestHandler(ListToolsRequestSchema, async () => {
return { tools };
});

server.setRequestHandler(ListToolsRequestSchema, async () => {
return { tools };
});
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
return executeTool(name, args);
});

server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
return executeTool(name, args);
});
return server;
}

/** Smithery sandbox: returns a server instance for capability scanning */
export function createSandboxServer() {
return createServer();
}

async function main() {
const server = createServer();

// Pre-warm the cache in background
getModels().catch(() => {
// Non-fatal — tools will retry on first call
Expand All @@ -38,7 +49,15 @@ async function main() {
console.error("tokencost MCP server started");
}

main().catch((error) => {
console.error("Failed to start server:", error);
process.exit(1);
});
// Only start stdio transport when executed directly (not imported for scanning)
const isDirectExecution =
import.meta.url === `file://${process.argv[1]}` ||
process.argv[1]?.endsWith("/tokencost-dev") ||
process.argv[1]?.endsWith("dist/index.js");

if (isDirectExecution) {
main().catch((error) => {
console.error("Failed to start server:", error);
process.exit(1);
});
}
3 changes: 2 additions & 1 deletion src/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";

const __dirname = dirname(fileURLToPath(import.meta.url));
const __dirname =
typeof import.meta?.url === "string" ? dirname(fileURLToPath(import.meta.url)) : process.cwd();
const CACHE_DIR = join(__dirname, "..", ".cache");
const CACHE_FILE = join(CACHE_DIR, "prices.json");
const SOURCE_URL =
Expand Down