@artinet/sdk
Version:
A TypeScript SDK for building collaborative AI agents.
123 lines (122 loc) • 4.29 kB
JavaScript
/**
* Copyright 2025 The Artinet Project
* SPDX-License-Identifier: Apache-2.0
*/
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { A2A } from "../../types/index.js";
import { formatJson } from "../../utils/utils.js";
export class BaseMCPService extends McpServer {
agent;
_registerBaseTools(uri = "agent://card") {
this.registerTool("send-message", {
title: "Send Message",
description: A2A.MessageSendParamsSchema.description,
inputSchema: A2A.MessageSendParamsSchema.shape,
outputSchema: A2A.TaskSchema.shape, //defaulting to task because unions are not supported
}, async (args) => {
const task = await this.agent.sendMessage(args);
return {
content: [
{
type: "text",
text: formatJson(task),
},
],
structuredContent: task,
};
});
this.registerTool("get-task", {
title: "Get Task",
description: A2A.TaskIdParamsSchema.description,
inputSchema: A2A.TaskIdParamsSchema.shape,
outputSchema: A2A.TaskSchema.shape,
}, async (args) => {
const task = await this.agent.getTask(args);
return {
content: [
{
type: "text",
text: formatJson(task),
},
],
structuredContent: task,
};
});
this.registerTool("cancel-task", {
title: "Cancel Task",
description: A2A.TaskIdParamsSchema.description,
inputSchema: A2A.TaskIdParamsSchema.shape,
outputSchema: A2A.TaskSchema.shape,
}, async (args) => {
const task = await this.agent.cancelTask(args);
return {
content: [
{
type: "text",
text: formatJson(task),
},
],
structuredContent: task,
};
});
this.registerResource("agent-card", uri, {
title: "Agent Card",
description: A2A.AgentCardSchema.description,
mimeType: "application/json",
}, async (uri) => {
return {
contents: [
{
uri: uri.href,
text: formatJson(await this.agent.getAgentCard()),
mimeType: "application/json",
},
],
};
});
}
constructor({ serverInfo, agent, options, agentCardUri = "agent://card", }) {
super(serverInfo, options);
this.agent = agent;
this._registerBaseTools(agentCardUri);
}
async stop() {
await super.close();
await this.agent.stop();
}
static create({ serverInfo, agent, options, agentCardUri = "agent://card", }) {
const instance = new BaseMCPService({
serverInfo,
agent,
options,
agentCardUri,
});
return new Proxy(instance, {
get(target, prop, receiver) {
if (prop in target) {
return Reflect.get(target, prop, receiver);
}
if (!target.agent) {
return undefined;
}
if (prop in target.agent) {
const value = target.agent[prop];
if (typeof value === "function") {
return value.bind(target.agent);
}
return value;
}
return Reflect.get(target, prop, receiver);
},
set(target, prop, value, receiver) {
if (prop in target.agent) {
target.agent[prop] = value;
return true;
}
return Reflect.set(target, prop, value, receiver);
},
});
}
}
export const createMCPService = BaseMCPService.create;
export const createMCPAgent = createMCPService;