Skip to content

add support for function/asset upload #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/mcp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@
"@apidevtools/swagger-parser": "^10.1.1",
"@modelcontextprotocol/sdk": "^1.7.0",
"@twilio-alpha/openapi-mcp-server": "0.4.0",
"form-data": "^4.0.2",
"inquirer": "^12.5.0",
"minimist": "^1.2.8",
"openapi-types": "^12.1.3"
},
"devDependencies": {
"@types/form-data": "^2.2.1",
"@types/minimist": "^1.2.5",
"@types/node": "^22.13.10",
"@typescript-eslint/eslint-plugin": "^7.18.0",
Expand Down
22 changes: 22 additions & 0 deletions packages/mcp/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {

import { Credentials } from '@app/types';
import { toolRequiresAccountSid } from '@app/utils';
import { loadAdditionalTools, uploadFunction, uploadAsset } from '@app/tools';

type Configuration = {
server: {
Expand Down Expand Up @@ -104,6 +105,21 @@ export default class TwilioOpenAPIMCPServer extends OpenAPIMCPServer {
throw new Error(`Resource ${name} not found`);
}

protected async makeRequest(
id: string,
api: API,
body?: Record<string, unknown>,
) {
if (id === uploadFunction.name && body) {
return uploadFunction.uploadFunctionExecution(body, this.http);
}
if (id === uploadAsset.name && body) {
return uploadAsset.uploadAssetExecution(body, this.http);
}

return super.makeRequest(id, api, body);
}

/**
* Loads resources for the server
* @returns
Expand All @@ -128,5 +144,11 @@ export default class TwilioOpenAPIMCPServer extends OpenAPIMCPServer {
this.tools.set(id, updatedTool);
}
}

const additionalTools = loadAdditionalTools(this.configuration?.filters);
for (const [id, { tool, api }] of additionalTools) {
this.tools.set(id, tool);
this.apis.set(id, api);
}
}
}
34 changes: 34 additions & 0 deletions packages/mcp/src/tools/additionalTools.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Tool } from '@modelcontextprotocol/sdk/types';
import { API, ToolFilters } from '@twilio-alpha/openapi-mcp-server';
import { uploadFunctionDefinition, uploadFunctionAPI } from './uploadFunction';
import { uploadAssetDefinition, uploadAssetAPI } from './uploadAsset';

type Additional = {
tool: Tool;
api: API;
};
export default function loadAdditionalTools(
filters?: ToolFilters,
): Map<string, Additional> {
const tools: Map<string, Additional> = new Map();

const shouldIncludeServerless =
!filters ||
(filters.services &&
filters.services.some((s) => s.includes('serverless'))) ||
(filters.tags && filters.tags.some((t) => t.includes('Serverless')));

if (shouldIncludeServerless) {
tools.set(uploadFunctionDefinition.name, {
tool: uploadFunctionDefinition,
api: uploadFunctionAPI,
});

tools.set(uploadAssetDefinition.name, {
tool: uploadAssetDefinition,
api: uploadAssetAPI,
});
}

return tools;
}
3 changes: 3 additions & 0 deletions packages/mcp/src/tools/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default as loadAdditionalTools } from './additionalTools';
export * as uploadFunction from './uploadFunction';
export * as uploadAsset from './uploadAsset';
91 changes: 91 additions & 0 deletions packages/mcp/src/tools/uploadAsset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import FormData from 'form-data';
import { Tool } from '@modelcontextprotocol/sdk/types';
import { Http } from '@twilio-alpha/openapi-mcp-server/build/utils';
import { HttpResponse } from '@twilio-alpha/openapi-mcp-server/build/utils/http';
import { API } from '@twilio-alpha/openapi-mcp-server';

export const name = 'TwilioServerlessV1--UploadServerlessAsset';

export const uploadAssetExecution = async (
params: Record<string, any>,
http: Http,
): Promise<HttpResponse<unknown>> => {
const { serviceSid, assetSid, path, visibility, content, contentType } =
params;

try {
const serviceUrl = `https://serverless-upload.twilio.com/v1/Services/${serviceSid}`;
const uploadUrl = `${serviceUrl}/Assets/${assetSid}/Versions`;

const form = new FormData();
form.append('Path', path);
form.append('Visibility', visibility);
const buffer = Buffer.from(content, 'utf-8');
form.append('Content', buffer, {
filename: 'asset',
contentType,
});

return await http.upload<{ sid: string }>(uploadUrl, form);
} catch (error) {
return {
ok: false,
statusCode: 500,
// @ts-ignore
error,
};
}
};

// these are unused - it's a stub
export const uploadAssetAPI: API = {
method: 'POST',
contentType: 'multipart/form-data',
path: 'fake',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will there be conflicting asset names here if it makes more than one in a session for the same service?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a stub because we need an apis inside the server during the lookup (they are used for making the HTTP call) but for upload, we have hand-written upload method so the apis is unused. But I still have to define this stub other we get a Tool not found

};

export const uploadAssetDefinition: Tool = {
name,
description:
"Create a new Asset resource. Assets are static files like HTML, CSS, images, or client-side JavaScript files that can be referenced by your Serverless Functions or served directly to clients. You must create a Service before creating Assets. After creating an Asset, you'll need to create Asset Versions to add the actual content.",
inputSchema: {
type: 'object',
properties: {
serviceSid: {
type: 'string',
description:
'The SID of the Twilio Serverless Service where the asset will be uploaded',
},
assetSid: {
type: 'string',
description: 'The SID of the Asset to create a new version for',
},
path: {
type: 'string',
description: 'The HTTP path used to invoke the asset (e.g., "/thanos")',
},
visibility: {
type: 'string',
description:
'The visibility of the asset, typically "public" or "private"',
},
content: {
type: 'string',
description: 'The content of the Asset',
},
contentType: {
type: 'string',
description:
'The content type of the Asset being uploaded. This must match the actual content of the file.',
},
},
required: [
'serviceSid',
'assetSid',
'path',
'visibility',
'content',
'contentType',
],
},
};
Loading