@artinet/sdk
Version:
TypeScript SDK for Agentic Communication
75 lines • 2.6 kB
JavaScript
import { McpServer, } from "@modelcontextprotocol/sdk/server/mcp.js";
import { Protocol } from "../../types/services/protocol.js";
import { MCPRequestParamsSchema, } from "../../types/services/mcp/service.js";
export class MCPService extends McpServer {
name;
protocol;
engine;
constructor({ serverInfo, engine, options, skills, }) {
super(serverInfo, options);
this.name = "mcp";
this.protocol = Protocol.MCP;
this.engine = engine;
if (skills) {
this.initialize(skills);
}
}
initialize(skills) {
for (const skill of skills) {
const tool = MCPService.skillToTool(skill, MCPRequestParamsSchema.shape, MCPService.mcpFactory(this.engine));
super.tool(skill.name, skill.description ?? "", tool.paramsSchemaOrAnnotations, tool.cb);
}
}
async execute({ executionContext, }) {
if (!executionContext.requestContext) {
throw new Error("No request context");
}
const { request, transport, response } = executionContext.requestContext;
const validTransport = transport;
if (!validTransport) {
throw new Error("Invalid transport");
}
await super.connect(validTransport);
await validTransport.handleRequest(request, response, request.body);
}
async stop() {
await super.close();
}
static mcpFactory(engine) {
return async (args) => {
const context = {
id: "",
protocol: Protocol.MCP,
getRequestParams: () => args,
isCancelled: () => false,
};
const generator = await engine(context);
let finalResult;
for await (const event of generator) {
finalResult = event;
}
await Promise.resolve();
return JSON.stringify(finalResult);
};
}
static skillToTool(skill, argShape, agentHandler) {
const skillTool = {
name: skill.name,
description: skill.description,
paramsSchemaOrAnnotations: argShape,
cb: async (...args) => {
const toolResult = {
content: [
{
type: "text",
text: await agentHandler(...args),
},
],
};
return toolResult;
},
};
return skillTool;
}
}
//# sourceMappingURL=service.js.map