@solana-agent-kit/adapter-mcp
Version:
Create MCP servers with the Solana Agent Kit
135 lines (133 loc) • 4.29 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
createMcpServer: () => createMcpServer,
startMcpServer: () => startMcpServer,
zodToMCPShape: () => zodToMCPShape
});
module.exports = __toCommonJS(index_exports);
var import_mcp = require("@modelcontextprotocol/sdk/server/mcp.js");
var import_stdio = require("@modelcontextprotocol/sdk/server/stdio.js");
var import_zod = require("zod");
function isZodOptional(schema) {
return schema instanceof import_zod.z.ZodOptional;
}
function isZodObject(schema) {
return schema instanceof import_zod.z.ZodObject || schema?._def?.typeName === "ZodObject";
}
function zodToMCPShape(schema) {
if (!isZodObject(schema)) {
throw new Error("MCP tools require an object schema at the top level");
}
const shape = schema.shape;
const result = {};
for (const [key, value] of Object.entries(shape)) {
result[key] = isZodOptional(value) ? value.unwrap() : value;
}
return {
result,
keys: Object.keys(result)
};
}
function createMcpServer(actions, solanaAgentKit, options) {
const server = new import_mcp.McpServer({
name: options.name,
version: options.version
});
for (const [_key, action] of Object.entries(actions)) {
const { result } = zodToMCPShape(action.schema);
server.tool(action.name, action.description, result, async (params) => {
try {
const result2 = await action.handler(solanaAgentKit, params);
return {
content: [
{
type: "text",
text: JSON.stringify(result2, null, 2)
}
]
};
} catch (error) {
console.error("error", error);
return {
isError: true,
content: [
{
type: "text",
text: error instanceof Error ? error.message : "Unknown error occurred"
}
]
};
}
});
if (action.examples && action.examples.length > 0) {
server.prompt(
`${action.name}-examples`,
{
showIndex: import_zod.z.string().optional().describe("Example index to show (number)")
},
(args) => {
const showIndex = args.showIndex ? parseInt(args.showIndex) : void 0;
const examples = action.examples.flat();
const selectedExamples = typeof showIndex === "number" ? [examples[showIndex]] : examples;
const exampleText = selectedExamples.map(
(ex, idx) => `
Example ${idx + 1}:
Input: ${JSON.stringify(ex.input, null, 2)}
Output: ${JSON.stringify(ex.output, null, 2)}
Explanation: ${ex.explanation}
`
).join("\n");
return {
messages: [
{
role: "user",
content: {
type: "text",
text: `Examples for ${action.name}:
${exampleText}`
}
}
]
};
}
);
}
}
return server;
}
async function startMcpServer(actions, solanaAgentKit, options) {
try {
const server = createMcpServer(actions, solanaAgentKit, options);
const transport = new import_stdio.StdioServerTransport();
await server.connect(transport);
return server;
} catch (error) {
console.error("Error starting MCP server", error);
throw error;
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createMcpServer,
startMcpServer,
zodToMCPShape
});