Skip to content

Commit

Permalink
change loadings
Browse files Browse the repository at this point in the history
  • Loading branch information
ggagosh committed Dec 25, 2024
1 parent 6d989c7 commit df43146
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 96 deletions.
143 changes: 84 additions & 59 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
type CallToolResult,
ListResourceTemplatesRequestSchema,
ListResourcesRequestSchema,
ListToolsRequestSchema,
ReadResourceRequestSchema,
type Resource,
type ResourceTemplate,
type Tool,
} from "@modelcontextprotocol/sdk/types.js";
import { Zodios, type ZodiosInstance, isErrorFromAlias } from "@zodios/core";
import { Zodios, type ZodiosInstance } from "@zodios/core";
import { ApiDefinition } from "./types.js";

const { values } = parseArgs({
Expand Down Expand Up @@ -44,6 +48,8 @@ class DrupalMcpServer {
private readonly server: Server;
private readonly api: ZodiosInstance<typeof ApiDefinition>;
private _tools: Array<Tool> = [];
private _resourceTemplates: Array<ResourceTemplate> = [];
private _resources: Array<Resource> = [];

constructor() {
this.server = new Server(
Expand All @@ -54,60 +60,66 @@ class DrupalMcpServer {
{
capabilities: {
tools: {},
resources: {},
},
},
);

this.api = new Zodios(DRUPAL_BASE_URL as string, ApiDefinition);
}

private setupResourceHandlers(): void {
if (this._resourceTemplates.length > 0) {
console.error(this._resourceTemplates);

this.server.setRequestHandler(
ListResourceTemplatesRequestSchema,
async () => ({
resourceTemplates: this._resourceTemplates,
}),
);
}
if (this._resources.length > 0) {
this.server.setRequestHandler(ListResourcesRequestSchema, async () => ({
resources: this._resources,
}));
}

this.server.setRequestHandler(
ReadResourceRequestSchema,
async (request) => {
const response = await this.api.post("/mcp/post", {
jsonrpc: "2.0",
id: 2,
method: "resources/read",
params: {
uri: request.params.uri,
},
});

return response.result;
},
);
}

private setupToolHandlers(): void {
this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: this._tools,
}));

this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
if (
this._tools.length === 0 ||
!this._tools.find((t) => t.name === request.params.name)
) {
return {
content: {
mimeType: "text/plain",
text: `Tool "${request.params.name}" not found`,
},
isError: true,
};
}

try {
const response = await this.api.executeTool(request.params.arguments, {
params: {
toolId: request.params.name,
},
});
const response = await this.api.post("/mcp/post", {
jsonrpc: "2.0",
id: 2,
method: "tools/call",
params: {
name: request.params.name,
arguments: request.params.arguments,
},
});
console.error(response.result);

return {
content: [
{
type: "text",
text: JSON.stringify(response, null, 2),
},
],
} satisfies CallToolResult;
} catch (error) {
if (isErrorFromAlias(ApiDefinition, "executeTool", error)) {
return {
content: {
mimeType: "text/plain",
text: `Drupal API error: ${error.message}`,
},
isError: true,
};
}

throw error;
}
return response.result;
});
}

Expand All @@ -124,26 +136,39 @@ class DrupalMcpServer {
}

async init() {
try {
const initInfo = await this.api.getInitializationInfo();
this._tools = initInfo.tools;

if (this._tools.length > 0) {
this.setupToolHandlers();
}

this.setupErrorHandling();
const [tools, resourceTemplates, resources] = await Promise.all([
this.api.post("/mcp/post", {
jsonrpc: "2.0",
id: 0,
method: "tools/list",
}),
this.api.post("/mcp/post", {
jsonrpc: "2.0",
id: 1,
method: "resources/templates/list",
}),
this.api.post("/mcp/post", {
jsonrpc: "2.0",
id: 2,
method: "resources/list",
}),
]);

this._tools = tools.result.tools;
this._resourceTemplates = resourceTemplates.result.resourceTemplates;
this._resources = resources.result.resources;

if (this._tools.length > 0) {
this.setupToolHandlers();
}

return this;
} catch (error) {
if (isErrorFromAlias(ApiDefinition, "getInitializationInfo", error)) {
console.error(`Drupal API error: ${error.message}`);
if (this._resourceTemplates.length > 0 || this._resources.length > 0) {
this.setupResourceHandlers();
}

process.exit(1);
}
this.setupErrorHandling();

throw error;
}
return this;
}

async run() {
Expand Down
39 changes: 2 additions & 37 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ToolSchema } from "@modelcontextprotocol/sdk/types.js";
import { makeApi } from "@zodios/core";
import { z } from "zod";

Expand All @@ -9,50 +8,16 @@ declare module "bun" {
}

export const ApiDefinition = makeApi([
{
method: "get",
path: "/mcp/init",
alias: "getInitializationInfo",
description: "Get Initial Information when MCP server initializes",
response: z.object({
tools: ToolSchema.array(),
}),
errors: [
{
status: "default",
schema: z.object({
message: z.string(),
}),
},
],
},
{
method: "post",
path: "/mcp/tools/execute/:toolId",
alias: "executeTool",
description: "Execute a tool",
path: "/mcp/post",
parameters: [
{
name: "toolId",
type: "Path",
schema: z.string(),
},
{
name: "arguments",
type: "Body",
schema: z.any(),
},
],
response: z.object({
result: z.any(),
}),
errors: [
{
status: "default",
schema: z.object({
message: z.string(),
}),
},
],
response: z.any(),
},
]);

0 comments on commit df43146

Please sign in to comment.