#!/usr/bin/env node
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
handleSearchOptions,
searchOptionsInputSchema,
} from "./tools/search-options.ts";
import {
handleSearchPackages,
searchPackagesInputSchema,
} from "./tools/search-packages.ts";
const server = new McpServer({
name: "nixos-search",
version: "1.0.0",
});
// Register the search_nixos_options tool
server.registerTool(
"search_nixos_options",
{
description:
"Search for NixOS configuration options by query. Returns matching options with their types, defaults, and descriptions.",
inputSchema: searchOptionsInputSchema,
},
async (input) => {
try {
// Validate input using Zod schema
const validatedInput = searchOptionsInputSchema.parse(input);
const result = await handleSearchOptions(validatedInput);
return {
content: [
{
type: "text" as const,
text: result,
},
],
};
} catch (error) {
const message =
error instanceof Error ? error.message : "Unknown error";
return {
content: [
{
type: "text" as const,
text: `Error: ${message}`,
},
],
};
}
}
);
// Register the search_nix_packages tool
server.registerTool(
"search_nix_packages",
{
description:
"Search for Nix packages by query. Returns matching packages with their versions, descriptions, and maintainers.",
inputSchema: searchPackagesInputSchema,
},
async (input) => {
try {
// Validate input using Zod schema
const validatedInput = searchPackagesInputSchema.parse(input);
const result = await handleSearchPackages(validatedInput);
return {
content: [
{
type: "text" as const,
text: result,
},
],
};
} catch (error) {
const message =
error instanceof Error ? error.message : "Unknown error";
return {
content: [
{
type: "text" as const,
text: `Error: ${message}`,
},
],
};
}
}
);
// Set up stdio transport
const transport = new StdioServerTransport();
await server.connect(transport);
// Log that server is running (to stderr, not stdout which is reserved for JSON-RPC)
console.error("NixOS MCP Server started successfully");