slite-mcp-server
Version:
'Slite MCP server'
114 lines (112 loc) • 4.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.main = exports.displayHelp = exports.parseArgs = void 0;
const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js");
const zod_1 = require("zod");
const commander_1 = require("commander");
const package_json_1 = require("../package.json");
const client_1 = require("./client");
const DEFAULT_API_BASE_URL = 'https://api.slite.com/v1';
const ArgsType = zod_1.z.object({
apiKey: zod_1.z.preprocess((value) => value ?? process.env.SLITE_API_KEY, zod_1.z.string({
message: 'Slite API key not provided. Please either pass it as an argument --api-key=$KEY or set the SLITE_API_KEY environment variable.',
})),
baseUrl: zod_1.z
.string()
.url({ message: 'Base url must be a valid url.' })
.default(process.env.SLITE_API_BASE_URL ?? DEFAULT_API_BASE_URL),
});
commander_1.program
.name(package_json_1.name)
.description(package_json_1.description)
.version(package_json_1.version)
.option('--api-key <key>', 'Your Slite API key (required unless SLITE_API_KEY env is set)')
.option('--base-url <url>', 'Custom API base URL (default: https://api.slite.com/v1)')
.option('-h, --help');
const parseArgs = (args) => {
const result = ArgsType.safeParse(args);
if (!result.success) {
throw new Error(result.error.errors.map((e) => e.message).join('|'));
}
return result.data;
};
exports.parseArgs = parseArgs;
const bindTool = (server, sliteClient) => {
// Add search-notes tool
server.tool('search-notes', 'Searches notes in Slite based on a query and returns the top search results.', {
query: zod_1.z.string().describe('The search query to perform.'),
}, async ({ query }) => {
const { results } = await sliteClient.search({ query });
return {
content: [{ type: 'text', text: JSON.stringify(results) }],
};
});
// Add ask tool
server.tool('ask-slite', 'Asks a question to Slite and returns an answer with sources.', {
question: zod_1.z.string().describe('The question to ask Slite.'),
}, async ({ question }) => {
const { answer, sources } = await sliteClient.ask({ question });
return {
content: [
{
type: 'text',
text: `${answer}\n\nSources:\n${sources.map(s => `- ${s.title}: ${s.url}`).join('\n')}`
}
],
};
});
// Add get-note tool
server.tool('get-note', 'Retrieves a specific note from Slite by its ID.', {
noteId: zod_1.z.string().describe('The ID of the note to retrieve.'),
}, async ({ noteId }) => {
const note = await sliteClient.getNote({ noteId });
return {
content: [{ type: 'text', text: JSON.stringify(note) }],
};
});
// Add get-note-children tool
server.tool('get-note-children', 'Retrieves all child notes of a specific note from Slite.', {
noteId: zod_1.z.string().describe('The ID of the parent note.'),
}, async ({ noteId }) => {
const children = await sliteClient.getNoteChildren({ noteId });
return {
content: [{ type: 'text', text: JSON.stringify(children) }],
};
});
};
const displayHelp = () => {
console.log(`
Usage: npx -y slite-mcp [options]
Options:
--api-key Your Slite API key (required unless SLITE_API_KEY env is set)
--base-url Custom API base URL (default: https://api.slite.com/v1)
--help, -h Show this help text
`);
process.exit(0);
};
exports.displayHelp = displayHelp;
const main = async () => {
const args = commander_1.program.parse(process.argv).opts();
if (args.help) {
(0, exports.displayHelp)();
}
const { apiKey, baseUrl } = (0, exports.parseArgs)(args);
const server = new mcp_js_1.McpServer({
name: package_json_1.name,
version: package_json_1.version,
});
bindTool(server, new client_1.SliteClient({
apiKey,
baseUrl,
}));
await server.connect(new stdio_js_1.StdioServerTransport());
};
exports.main = main;
if (require.main === module) {
(0, exports.main)().catch((e) => {
console.error(`An error occurred: ${e.message}`);
process.exit(1);
});
}