All files convo.js

72.46% Statements 50/69
100% Branches 2/2
33.33% Functions 1/3
72.46% Lines 50/69

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 701x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x               1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x  
"use strict";
import bag from 'bagofcli';
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
 
/**
 * Convo is a class that runs Model Context Protocol (MCP) server
 * with registered tools having node handler function and info metadata.
 */
class Convo {
 
  /**
   * Initialise MCP server and register MCP tools.
   * @param {String} name: name of the MCP server
   * @param {String} version: version of the MCP server
   * @param {Array} tools: array of MCP tools to be registered
   * @param {Object} toolOpts: options object that contains properties
   *                           to be passed to each tool handler function.
   */
  constructor(name, version, tools, toolOpts) {
 
    // Callback function to handle Nestor action result.
    // It resolves or rejects the Promise based on the action result.
    // It formats the result into a content text that can be used in the response.
    function _actionCb(resolve, reject, resultText) {
      return (err, result) => {
        if (err) {
          reject(err);
        } else {
          resolve({
            content: [{ type: 'text', text: resultText(result)}]
          });
        }
      };
    };
 
    bag.logStepHeading(`Initialising MCP server ${name} ${version}`);
    this.server = new McpServer({
      name: name,
      version: version,
    });
 
    // Register each tool with its info and handler function.
    // The handler function is provided with toolOpts.
    // Any resource needed by the tool handler can be passed through toolOpts.
    tools.forEach(tool => {
      bag.logStepHeading(`Registering tool: ${tool.info.name}`);
      this.server.tool(
        tool.info.name,
        tool.info.description,
        tool.info.parameters,
        tool.handler(toolOpts, _actionCb)
      );
    });
  }
 
  /**
   * Run the MCP server.
   */
  async run() {
    const transport = new StdioServerTransport();
    await this.server.connect(transport);
  }
 
}
 
export {
  Convo as default
};