@agentset/mcp
Version:
MCP server for Agentset, an open-source platform for Retrieval-Augmented Generation (RAG). Designed for developers who want to build intelligent, document-based applications quickly and efficiently.
115 lines (111 loc) • 3.82 kB
JavaScript
// src/index.ts
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { Agentset } from "agentset";
import { Command } from "commander";
import nodeFetch from "node-fetch";
import { z } from "zod";
// src/utils.ts
import { readFileSync } from "fs";
import { dirname, join } from "path";
import { fileURLToPath } from "url";
var getVersion = () => {
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const packageJsonPath = join(__dirname, "..", "package.json");
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf8"));
const packageVersion = packageJson.version;
return packageVersion;
};
var defaultDescription = `
Look up information in the Knowledge Base. Use this tool when you need to:
- Find relevant documents or information on specific topics
- Retrieve company policies, procedures, or guidelines
- Access product specifications or technical documentation
- Get contextual information to answer company-specific questions
- Find historical data or information about projects
`.trim();
var parseOptions = (options2) => {
if (!process.env.AGENTSET_API_KEY)
throw new Error("AGENTSET_API_KEY not set");
const API_KEY2 = process.env.AGENTSET_API_KEY;
const NAMESPACE_ID2 = options2.namespace || process.env.AGENTSET_NAMESPACE_ID;
if (!NAMESPACE_ID2)
throw new Error("Either pass --namespace or set AGENTSET_NAMESPACE_ID");
const tenantId2 = options2.tenant;
console.error("Using namespace: ", NAMESPACE_ID2);
if (tenantId2) {
console.error("Using tenant: ", tenantId2);
}
if (options2.description) {
console.error("Using overridden description");
}
const description2 = options2.description || defaultDescription;
return {
API_KEY: API_KEY2,
NAMESPACE_ID: NAMESPACE_ID2,
tenantId: tenantId2,
description: description2
};
};
// src/index.ts
var program = new Command();
program.version(getVersion(), "-v, --version", "output the current version").option(
"-d, --description [description]",
"override the default tool description"
).option("-t, --tenant [tenant]", "specify the Agentset tenant id to use").option(
"--ns, --namespace [namespace]",
"specify the Agentset namespace id to use"
).parse(process.argv);
var options = program.opts();
var { API_KEY, NAMESPACE_ID, tenantId, description } = parseOptions(options);
var server = new McpServer({
name: "Agentset MCP",
version: "1.0.0"
});
var agentset = new Agentset({
apiKey: API_KEY,
fetcher: (url, init) => {
return nodeFetch(
typeof url === "string" ? url : url instanceof URL ? url.toString() : url.url,
init
);
}
});
var ns = agentset.namespace(NAMESPACE_ID);
server.tool(
"knowledge-base-retrieve",
description,
{
query: z.string().describe("The query to search for data in the Knowledge Base"),
topK: z.number().describe("The maximum number of results to return. Defaults to 10.").min(1).max(100).optional().default(10),
rerank: z.boolean().describe(
"Whether to rerank the results based on relevance. Defaults to true."
).optional().default(true)
},
async ({ query, topK, rerank }) => {
const result = await ns.search(
query,
{
topK,
rerank
},
tenantId ? { tenantId } : void 0
);
const content = result.map((item) => ({
type: "text",
text: item.text
}));
return { content };
}
);
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("Agentset MCP Server running on stdio");
}
main().catch((error) => {
console.error("Fatal error in main():", error);
process.exit(1);
});