Skip to content

Commit

Permalink
add resolutions & update tool registration logic
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianescutia committed Jan 7, 2025
1 parent b3f629a commit 3b26797
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 21 deletions.
31 changes: 28 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@la-rebelion/mcp-server",
"version": "0.1.2",
"version": "0.2.0",
"description": "A Model Context Protocol Server facade to simplify the implementation of agents",
"type": "module",
"author": {
Expand All @@ -18,13 +18,35 @@
"type": "git",
"url": "https://github.com/la-rebelion/mcp-server"
},
"keywords": [
"mcp",
"modelcontextprotocol",
"server",
"agentic",
"facade",
"ai"
],
"engines": {
"node": ">=16"
"node": ">=18"
},
"license": "MIT",
"bin": {
"server": "./build/index.js"
},
"main": "./build/index.js",
"exports": {
".": {
"import": "./build/index.js",
"types": "./build/index.d.ts"
}
},
"typesVersions": {
"*": {
"*": [
"./build/*"
]
}
},
"scripts": {
"build": "tsc",
"postbuild": "node -e \"require('fs').chmodSync('build/index.js', '755')\"",
Expand All @@ -51,5 +73,8 @@
"@modelcontextprotocol/sdk": "^1.1.0",
"zod": "^3.24.1",
"zod-to-json-schema": "^3.24.1"
},
"resolutions": {
"strip-ansi": "6.0.1"
}
}
}
19 changes: 15 additions & 4 deletions src/MCPServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,28 @@ class MCPServer {
});
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
const tool = this.getTool(request.params.name);
this.server.sendLoggingMessage({
level: "info",
data: `Executing tool ${tool.toolSchema.name}`,
});

return await tool.execute(request.params.arguments);
});
}
registerTool(name: string, tool: any) {
const instance = new tool();
registerTool(name: string, tool: Tool) {
// const instance = new tool();
// must be an instance that implements the Tool interface
if (!(instance instanceof Tool)) {
if (!(tool instanceof Tool)) {
throw new Error(`Tool ${name} is not an instance of Tool, please extend Tool or review the class definition`);
}
this.tools.set(name, instance);
this.tools.set(name, tool);
}

/**
* Pull a tool from the registered tools
* @param name The name of the tool to get from registered tools
* @returns the tool instance
*/
private getTool(name: string): Tool {
const tool = this.tools.get(name);
if (!tool) {
Expand Down
3 changes: 3 additions & 0 deletions src/Tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export const _ToolSchema = z.object({
});
export type ToolSchema = z.infer<typeof _ToolSchema>;
export abstract class Tool {
constructor() {
this.init();
}
toolSchema!: ToolSchema;
abstract init(): void;
abstract execute(input: any): Promise<any>;
Expand Down
16 changes: 2 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,5 @@

import MCPServer from './MCPServer.js';

const myServer = new MCPServer('La Rebelion MCP Server', '1.0.0');

async function main() {
console.log("Empty server, implement your tools.");
console.log("Learn how to create tools: https://www.npmjs.com/package/@la-rebelion/mcp-server");
// Register your tools, i.e.:
// myServer.registerTool("echo", EchoTool);
await myServer.run();
}

main().catch((error) => {
console.error("Server error:", error);
process.exit(1);
});
export { MCPServer };
export { Tool } from './Tool.js';

0 comments on commit 3b26797

Please sign in to comment.