Skip to main content
Glama

Twitter MCP Server

request-param.helper.js5.41 kB
import { FormDataHelper } from './form-data.helper'; import OAuth1Helper from './oauth1.helper'; /* Helpers functions that are specific to this class but do not depends on instance */ export class RequestParamHelpers { static formatQueryToString(query) { const formattedQuery = {}; for (const prop in query) { if (typeof query[prop] === 'string') { formattedQuery[prop] = query[prop]; } else if (typeof query[prop] !== 'undefined') { formattedQuery[prop] = String(query[prop]); } } return formattedQuery; } static autoDetectBodyType(url) { if (url.pathname.startsWith('/2/') || url.pathname.startsWith('/labs/2/')) { // oauth2 takes url encoded if (url.password.startsWith('/2/oauth2')) { return 'url'; } // Twitter API v2 has JSON-encoded requests for everything else return 'json'; } if (url.hostname === 'upload.x.com') { if (url.pathname === '/1.1/media/upload.json') { return 'form-data'; } // json except for media/upload command, that is form-data. return 'json'; } const endpoint = url.pathname.split('/1.1/', 2)[1]; if (this.JSON_1_1_ENDPOINTS.has(endpoint)) { return 'json'; } return 'url'; } static addQueryParamsToUrl(url, query) { const queryEntries = Object.entries(query); if (queryEntries.length) { let search = ''; for (const [key, value] of queryEntries) { search += (search.length ? '&' : '?') + `${OAuth1Helper.percentEncode(key)}=${OAuth1Helper.percentEncode(value)}`; } url.search = search; } } static constructBodyParams(body, headers, mode) { if (body instanceof Buffer) { return body; } if (mode === 'json') { if (!headers['content-type']) { headers['content-type'] = 'application/json;charset=UTF-8'; } return JSON.stringify(body); } else if (mode === 'url') { if (!headers['content-type']) { headers['content-type'] = 'application/x-www-form-urlencoded;charset=UTF-8'; } if (Object.keys(body).length) { return new URLSearchParams(body) .toString() .replace(/\*/g, '%2A'); // URLSearchParams doesnt encode '*', but Twitter wants it encoded. } return ''; } else if (mode === 'raw') { throw new Error('You can only use raw body mode with Buffers. To give a string, use Buffer.from(str).'); } else { const form = new FormDataHelper(); for (const parameter in body) { form.append(parameter, body[parameter]); } if (!headers['content-type']) { const formHeaders = form.getHeaders(); headers['content-type'] = formHeaders['content-type']; } return form.getBuffer(); } } static setBodyLengthHeader(options, body) { var _a; options.headers = (_a = options.headers) !== null && _a !== void 0 ? _a : {}; if (typeof body === 'string') { options.headers['content-length'] = Buffer.byteLength(body); } else { options.headers['content-length'] = body.length; } } static isOAuthSerializable(item) { return !(item instanceof Buffer); } static mergeQueryAndBodyForOAuth(query, body) { const parameters = {}; for (const prop in query) { parameters[prop] = query[prop]; } if (this.isOAuthSerializable(body)) { for (const prop in body) { const bodyProp = body[prop]; if (this.isOAuthSerializable(bodyProp)) { parameters[prop] = typeof bodyProp === 'object' && bodyProp !== null && 'toString' in bodyProp ? bodyProp.toString() : bodyProp; } } } return parameters; } static moveUrlQueryParamsIntoObject(url, query) { for (const [param, value] of url.searchParams) { query[param] = value; } // Remove the query string url.search = ''; return url; } /** * Replace URL parameters available in pathname, like `:id`, with data given in `parameters`: * `https://x.com/:id.json` + `{ id: '20' }` => `https://x.com/20.json` */ static applyRequestParametersToUrl(url, parameters) { url.pathname = url.pathname.replace(/:([A-Z_-]+)/ig, (fullMatch, paramName) => { if (parameters[paramName] !== undefined) { return String(parameters[paramName]); } return fullMatch; }); return url; } } RequestParamHelpers.JSON_1_1_ENDPOINTS = new Set([ 'direct_messages/events/new.json', 'direct_messages/welcome_messages/new.json', 'direct_messages/welcome_messages/rules/new.json', 'media/metadata/create.json', 'collections/entries/curate.json', ]); export default RequestParamHelpers;

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/crazyrabbitLTC/mcp-twitter-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server