@pipeworx/mcp-twitch 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Pipeworx
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # mcp-twitch
2
+
3
+ Twitch MCP — wraps the Twitch Helix API (read-only)
4
+
5
+ Part of [Pipeworx](https://pipeworx.io) — an MCP gateway connecting AI agents to 751+ live data sources.
6
+
7
+ ## Tools
8
+
9
+ | Tool | Description |
10
+ |------|-------------|
11
+
12
+ ## Quick Start
13
+
14
+ Add to your MCP client (Claude Desktop, Cursor, Windsurf, etc.):
15
+
16
+ ```json
17
+ {
18
+ "mcpServers": {
19
+ "twitch": {
20
+ "url": "https://gateway.pipeworx.io/twitch/mcp"
21
+ }
22
+ }
23
+ }
24
+ ```
25
+
26
+ Or connect to the full Pipeworx gateway for access to all 751+ data sources:
27
+
28
+ ```json
29
+ {
30
+ "mcpServers": {
31
+ "pipeworx": {
32
+ "url": "https://gateway.pipeworx.io/mcp"
33
+ }
34
+ }
35
+ }
36
+ ```
37
+
38
+ ## Using with ask_pipeworx
39
+
40
+ Instead of calling tools directly, you can ask questions in plain English:
41
+
42
+ ```
43
+ ask_pipeworx({ question: "your question about Twitch data" })
44
+ ```
45
+
46
+ The gateway picks the right tool and fills the arguments automatically.
47
+
48
+ ## More
49
+
50
+ - [All tools and guides](https://github.com/pipeworx-io/examples)
51
+ - [pipeworx.io](https://pipeworx.io)
52
+
53
+ ## License
54
+
55
+ MIT
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@pipeworx/mcp-twitch",
3
+ "version": "0.1.0",
4
+ "description": "Twitch MCP — wraps the Twitch Helix API (read-only)",
5
+ "type": "module",
6
+ "main": "src/index.ts",
7
+ "types": "src/index.ts",
8
+ "keywords": ["mcp", "mcp-server", "model-context-protocol", "pipeworx", "twitch"],
9
+ "license": "MIT",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/pipeworx-io/mcp-twitch"
13
+ },
14
+ "scripts": {
15
+ "typecheck": "tsc --noEmit"
16
+ },
17
+ "devDependencies": {
18
+ "typescript": "^5.7.0"
19
+ }
20
+ }
package/server.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "$schema": "https://static.modelcontextprotocol.io/schemas/2025-12-11/server.schema.json",
3
+ "name": "io.github.pipeworx-io/twitch",
4
+ "title": "Twitch",
5
+ "description": "Twitch MCP — wraps the Twitch Helix API (read-only)",
6
+ "version": "0.1.0",
7
+ "websiteUrl": "https://pipeworx.io/packs/twitch",
8
+ "repository": {
9
+ "url": "https://github.com/pipeworx-io/mcp-twitch",
10
+ "source": "github"
11
+ },
12
+ "remotes": [
13
+ {
14
+ "type": "streamable-http",
15
+ "url": "https://gateway.pipeworx.io/twitch/mcp"
16
+ }
17
+ ]
18
+ }
package/src/index.ts ADDED
@@ -0,0 +1,310 @@
1
+ interface McpToolDefinition {
2
+ name: string;
3
+ description: string;
4
+ inputSchema: {
5
+ type: 'object';
6
+ properties: Record<string, unknown>;
7
+ required?: string[];
8
+ };
9
+ }
10
+
11
+ interface McpToolExport {
12
+ tools: McpToolDefinition[];
13
+ callTool: (name: string, args: Record<string, unknown>) => Promise<unknown>;
14
+ meter?: { credits: number };
15
+ cost?: Record<string, unknown>;
16
+ provider?: string;
17
+ }
18
+
19
+ /**
20
+ * Twitch MCP — wraps the Twitch Helix API (read-only)
21
+ *
22
+ * Tools:
23
+ * - top_games: the top games currently being streamed on Twitch
24
+ * - get_streams: live streams, optionally filtered by game name / language
25
+ * - search_channels: search Twitch channels/streamers by name
26
+ * - get_user: look up a Twitch user/channel by login (username)
27
+ *
28
+ * SPECIAL AUTH — Twitch uses OAuth client-credentials. The credential is
29
+ * passed as `_apiKey` in the form "CLIENT_ID:CLIENT_SECRET". We exchange it
30
+ * for a short-lived app access token (https://id.twitch.tv/oauth2/token) on
31
+ * every call — packs are stateless, so a fresh token per call is fine.
32
+ *
33
+ * _apiKey is OPTIONAL — omit it to use the shared Pipeworx Twitch credential.
34
+ */
35
+
36
+
37
+ const HELIX = 'https://api.twitch.tv/helix';
38
+ const TOKEN_URL = 'https://id.twitch.tv/oauth2/token';
39
+
40
+ // Exchange a Client-ID + Client-Secret pair for a short-lived app access
41
+ // token via the OAuth2 client-credentials grant. Returns the access_token.
42
+ async function getAppToken(clientId: string, clientSecret: string): Promise<string> {
43
+ const body = new URLSearchParams({
44
+ client_id: clientId,
45
+ client_secret: clientSecret,
46
+ grant_type: 'client_credentials',
47
+ });
48
+ const res = await fetch(TOKEN_URL, {
49
+ method: 'POST',
50
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
51
+ body: body.toString(),
52
+ });
53
+ if (!res.ok) {
54
+ const text = await res.text();
55
+ throw new Error(`Twitch token exchange failed (${res.status}): ${text}`);
56
+ }
57
+ const data = (await res.json()) as { access_token?: string; expires_in?: number };
58
+ if (!data.access_token) {
59
+ throw new Error('Twitch token exchange returned no access_token.');
60
+ }
61
+ return data.access_token;
62
+ }
63
+
64
+ // Helix GET helper. Non-2xx → structured { error, message }.
65
+ async function helixGet(
66
+ path: string,
67
+ clientId: string,
68
+ token: string,
69
+ ): Promise<{ ok: true; json: any } | { ok: false; error: number; message: string }> {
70
+ const res = await fetch(`${HELIX}${path}`, {
71
+ headers: {
72
+ 'Client-Id': clientId,
73
+ Authorization: `Bearer ${token}`,
74
+ },
75
+ });
76
+ if (!res.ok) {
77
+ const message = await res.text();
78
+ return { ok: false, error: res.status, message };
79
+ }
80
+ return { ok: true, json: await res.json() };
81
+ }
82
+
83
+ const KEY_DESC =
84
+ 'Optional — your own Twitch credentials as CLIENT_ID:CLIENT_SECRET for higher limits; omit to use the shared Pipeworx key.';
85
+
86
+ const tools: McpToolExport['tools'] = [
87
+ {
88
+ name: 'top_games',
89
+ description:
90
+ 'Get the top games currently being streamed on Twitch, ranked by number of viewers. Returns game id, name, and box art. Example: top_games({ first: 20 })',
91
+ inputSchema: {
92
+ type: 'object',
93
+ properties: {
94
+ first: {
95
+ type: 'number',
96
+ description: 'Number of games to return (default 20, max 100)',
97
+ },
98
+ _apiKey: { type: 'string', description: KEY_DESC },
99
+ },
100
+ },
101
+ },
102
+ {
103
+ name: 'get_streams',
104
+ description:
105
+ 'Get live Twitch streams, optionally filtered by game name and/or language. Returns streamer name, title, current viewer count, and thumbnail for each live stream. Omit `game` for the overall top live streams across Twitch. Example: get_streams({ game: "Just Chatting", first: 20, language: "en" })',
106
+ inputSchema: {
107
+ type: 'object',
108
+ properties: {
109
+ game: {
110
+ type: 'string',
111
+ description: 'Game NAME to filter by (e.g. "Fortnite", "Just Chatting"). Resolved to a game id automatically.',
112
+ },
113
+ first: {
114
+ type: 'number',
115
+ description: 'Number of streams to return (default 20, max 100)',
116
+ },
117
+ language: {
118
+ type: 'string',
119
+ description: 'Optional ISO-639-1 language code to filter by, e.g. "en", "es", "ja".',
120
+ },
121
+ _apiKey: { type: 'string', description: KEY_DESC },
122
+ },
123
+ },
124
+ },
125
+ {
126
+ name: 'search_channels',
127
+ description:
128
+ 'Search Twitch channels/streamers by name or keyword. Returns matching channels with display name, the game they stream, whether they are live now, title, and thumbnail. Example: search_channels({ query: "ninja", live_only: true })',
129
+ inputSchema: {
130
+ type: 'object',
131
+ properties: {
132
+ query: {
133
+ type: 'string',
134
+ description: 'Search query — channel/streamer name or keyword, e.g. "ninja", "valorant".',
135
+ },
136
+ first: {
137
+ type: 'number',
138
+ description: 'Number of channels to return (default 20, max 100)',
139
+ },
140
+ live_only: {
141
+ type: 'boolean',
142
+ description: 'If true, only return channels that are currently live.',
143
+ },
144
+ _apiKey: { type: 'string', description: KEY_DESC },
145
+ },
146
+ required: ['query'],
147
+ },
148
+ },
149
+ {
150
+ name: 'get_user',
151
+ description:
152
+ 'Look up a Twitch user/channel by login (username). Returns profile info: id, display name, description, view count, broadcaster type, account creation date, and profile image. Example: get_user({ login: "ninja" })',
153
+ inputSchema: {
154
+ type: 'object',
155
+ properties: {
156
+ login: {
157
+ type: 'string',
158
+ description: 'Twitch username (login), e.g. "ninja", "pokimane".',
159
+ },
160
+ _apiKey: { type: 'string', description: KEY_DESC },
161
+ },
162
+ required: ['login'],
163
+ },
164
+ },
165
+ ];
166
+
167
+ async function callTool(name: string, args: Record<string, unknown>): Promise<unknown> {
168
+ const combined = args._apiKey as string | undefined;
169
+ delete args._apiKey;
170
+
171
+ const [clientId, clientSecret] = (combined || '').split(':');
172
+ if (!clientId || !clientSecret) {
173
+ return {
174
+ error: 'api_key_required',
175
+ message: 'No Twitch credentials available (need CLIENT_ID:CLIENT_SECRET).',
176
+ };
177
+ }
178
+
179
+ const token = await getAppToken(clientId, clientSecret);
180
+
181
+ switch (name) {
182
+ case 'top_games':
183
+ return topGames(args, clientId, token);
184
+ case 'get_streams':
185
+ return getStreams(args, clientId, token);
186
+ case 'search_channels':
187
+ return searchChannels(args, clientId, token);
188
+ case 'get_user':
189
+ return getUser(args, clientId, token);
190
+ default:
191
+ throw new Error(`Unknown tool: ${name}`);
192
+ }
193
+ }
194
+
195
+ function clampFirst(value: unknown, fallback = 20): number {
196
+ const n = Number(value);
197
+ if (!Number.isFinite(n) || n <= 0) return fallback;
198
+ return Math.min(100, Math.floor(n));
199
+ }
200
+
201
+ async function topGames(args: Record<string, unknown>, clientId: string, token: string) {
202
+ const first = clampFirst(args.first);
203
+ const r = await helixGet(`/games/top?first=${first}`, clientId, token);
204
+ if (!r.ok) return { error: r.error, message: r.message };
205
+
206
+ const data = (r.json.data ?? []) as Array<{ id: string; name: string; box_art_url: string }>;
207
+ return {
208
+ games: data.map((g) => ({ id: g.id, name: g.name, box_art: g.box_art_url })),
209
+ };
210
+ }
211
+
212
+ async function getStreams(args: Record<string, unknown>, clientId: string, token: string) {
213
+ const first = clampFirst(args.first);
214
+ const game = args.game as string | undefined;
215
+ const language = args.language as string | undefined;
216
+
217
+ let path: string;
218
+ if (game) {
219
+ const gr = await helixGet(`/games?name=${encodeURIComponent(game)}`, clientId, token);
220
+ if (!gr.ok) return { error: gr.error, message: gr.message };
221
+ const gameId = gr.json.data?.[0]?.id as string | undefined;
222
+ if (!gameId) {
223
+ return { error: 'not_found', message: `No Twitch game found matching "${game}".` };
224
+ }
225
+ path = `/streams?game_id=${encodeURIComponent(gameId)}&first=${first}`;
226
+ } else {
227
+ path = `/streams?first=${first}`;
228
+ }
229
+ if (language) path += `&language=${encodeURIComponent(language)}`;
230
+
231
+ const r = await helixGet(path, clientId, token);
232
+ if (!r.ok) return { error: r.error, message: r.message };
233
+
234
+ const data = (r.json.data ?? []) as Array<{
235
+ id: string; user_name: string; game_name: string; title: string;
236
+ viewer_count: number; language: string; started_at: string; thumbnail_url: string;
237
+ }>;
238
+ return {
239
+ streams: data.map((s) => ({
240
+ id: s.id,
241
+ user_name: s.user_name,
242
+ game_name: s.game_name,
243
+ title: s.title,
244
+ viewer_count: s.viewer_count,
245
+ language: s.language,
246
+ started_at: s.started_at,
247
+ thumbnail: s.thumbnail_url,
248
+ })),
249
+ };
250
+ }
251
+
252
+ async function searchChannels(args: Record<string, unknown>, clientId: string, token: string) {
253
+ const query = args.query as string | undefined;
254
+ if (!query) {
255
+ return { error: 'invalid_args', message: 'search_channels requires a `query`.' };
256
+ }
257
+ const first = clampFirst(args.first);
258
+ let path = `/search/channels?query=${encodeURIComponent(query)}&first=${first}`;
259
+ if (args.live_only) path += `&live_only=true`;
260
+
261
+ const r = await helixGet(path, clientId, token);
262
+ if (!r.ok) return { error: r.error, message: r.message };
263
+
264
+ const data = (r.json.data ?? []) as Array<{
265
+ id: string; display_name: string; game_name: string; is_live: boolean;
266
+ title: string; started_at: string; thumbnail_url: string;
267
+ }>;
268
+ return {
269
+ channels: data.map((c) => ({
270
+ id: c.id,
271
+ display_name: c.display_name,
272
+ game_name: c.game_name,
273
+ is_live: c.is_live,
274
+ title: c.title,
275
+ started_at: c.started_at,
276
+ thumbnail: c.thumbnail_url,
277
+ })),
278
+ };
279
+ }
280
+
281
+ async function getUser(args: Record<string, unknown>, clientId: string, token: string) {
282
+ const login = args.login as string | undefined;
283
+ if (!login) {
284
+ return { error: 'invalid_args', message: 'get_user requires a `login` (Twitch username).' };
285
+ }
286
+ const r = await helixGet(`/users?login=${encodeURIComponent(login)}`, clientId, token);
287
+ if (!r.ok) return { error: r.error, message: r.message };
288
+
289
+ const u = (r.json.data ?? [])[0] as
290
+ | {
291
+ id: string; login: string; display_name: string; description: string;
292
+ view_count: number; broadcaster_type: string; created_at: string; profile_image_url: string;
293
+ }
294
+ | undefined;
295
+ if (!u) {
296
+ return { error: 'not_found', message: `No Twitch user found with login "${login}".` };
297
+ }
298
+ return {
299
+ id: u.id,
300
+ login: u.login,
301
+ display_name: u.display_name,
302
+ description: u.description,
303
+ view_count: u.view_count,
304
+ broadcaster_type: u.broadcaster_type,
305
+ created_at: u.created_at,
306
+ profile_image: u.profile_image_url,
307
+ };
308
+ }
309
+
310
+ export default { tools, callTool, meter: { credits: 1 } } satisfies McpToolExport;
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "ESNext",
5
+ "moduleResolution": "bundler",
6
+ "strict": true,
7
+ "esModuleInterop": true,
8
+ "skipLibCheck": true,
9
+ "outDir": "dist",
10
+ "rootDir": "src",
11
+ "declaration": true
12
+ },
13
+ "include": ["src"]
14
+ }