-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmcp-github-tools.ts
More file actions
executable file
·174 lines (152 loc) · 5.61 KB
/
Copy pathmcp-github-tools.ts
File metadata and controls
executable file
·174 lines (152 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env -S npm run tsn -T
/**
---
title: MCP Hub + Claude Code + GitHub
slug: mcp-github-tools
use_case: Connect Claude Code running in a devbox to GitHub tools through MCP Hub without exposing raw GitHub credentials to the devbox.
workflow:
- Create an MCP config for GitHub
- Store GitHub token as a Runloop secret
- Launch a devbox with MCP Hub wiring
- Install Claude Code and register MCP endpoint
- Run a Claude prompt through MCP tools
- Shutdown devbox and clean up cloud resources
tags:
- mcp
- devbox
- github
- commands
- cleanup
prerequisites:
- RUNLOOP_API_KEY
- GITHUB_TOKEN (GitHub PAT with repo scope)
- ANTHROPIC_API_KEY
run: GITHUB_TOKEN=ghp_xxx ANTHROPIC_API_KEY=sk-ant-xxx yarn tsn -T examples/mcp-github-tools.ts
test: yarn test:examples
---
*/
import { RunloopSDK } from '@runloop/api-client';
import { wrapRecipe, runAsCli } from './_harness';
import type { RecipeContext, RecipeOutput, ExampleCheck } from './types';
function uniqueName(prefix: string): string {
return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
}
const GITHUB_MCP_ENDPOINT = 'https://api.githubcopilot.com/mcp/';
export interface McpExampleOptions {
skipIfMissingCredentials?: boolean;
}
export async function recipe(ctx: RecipeContext, options: McpExampleOptions): Promise<RecipeOutput> {
const { cleanup } = ctx;
const sdk = new RunloopSDK({
bearerToken: process.env['RUNLOOP_API_KEY'],
});
const resourcesCreated: string[] = [];
const checks: ExampleCheck[] = [];
const githubToken = process.env['GITHUB_TOKEN'];
const anthropicKey = process.env['ANTHROPIC_API_KEY'];
if (!githubToken) {
throw new Error('Set GITHUB_TOKEN to a GitHub PAT with repo scope.');
}
if (!anthropicKey) {
throw new Error('Set ANTHROPIC_API_KEY for Claude Code.');
}
// Register GitHub's MCP server with Runloop
const mcpConfig = await sdk.mcpConfig.create({
name: uniqueName('github-example'),
endpoint: GITHUB_MCP_ENDPOINT,
// whitelist only the tools we need
allowed_tools: [
'get_me',
'search_pull_requests',
'get_pull_request',
'get_repository',
'get_file_contents',
],
description: 'GitHub MCP server - example',
});
resourcesCreated.push(`mcp_config:${mcpConfig.id}`);
cleanup.add(`mcp_config:${mcpConfig.id}`, () => sdk.mcpConfig.fromId(mcpConfig.id).delete());
// Store the GitHub PAT as a Runloop secret
const secretName = 'GITHUB_MCP_EXAMPLE';
const secret = await sdk.secret.create({
name: secretName,
value: githubToken,
});
resourcesCreated.push(`secret:${secretName}`);
cleanup.add(`secret:${secretName}`, () => secret.delete());
// Launch a devbox with MCP Hub wiring
const devbox = await sdk.devbox.create({
name: uniqueName('mcp-claude-code'),
launch_parameters: {
resource_size_request: 'SMALL',
keep_alive_time_seconds: 300,
},
mcp: {
MCP_SECRET: {
mcp_config: mcpConfig.id,
secret: secret,
},
},
});
resourcesCreated.push(`devbox:${devbox.id}`);
cleanup.add(`devbox:${devbox.id}`, () => sdk.devbox.fromId(devbox.id).shutdown());
// Install Claude Code
const installResult = await devbox.cmd.exec('npm install -g @anthropic-ai/claude-code');
checks.push({
name: 'install Claude Code',
passed: installResult.exitCode === 0,
details: installResult.exitCode === 0 ? 'installed' : await installResult.stderr(),
});
if (installResult.exitCode !== 0) {
return { resourcesCreated, checks };
}
// Register MCP Hub endpoint with Claude Code
// The devbox has RL_MCP_URL and RL_MCP_TOKEN environment variables set automatically
const addMcpResult = await devbox.cmd.exec(
'claude mcp add runloop-mcp --transport http "$RL_MCP_URL" --header "Authorization: Bearer $RL_MCP_TOKEN"',
);
checks.push({
name: 'register MCP Hub in Claude',
passed: addMcpResult.exitCode === 0,
details: addMcpResult.exitCode === 0 ? 'registered' : await addMcpResult.stderr(),
});
if (addMcpResult.exitCode !== 0) {
return { resourcesCreated, checks };
}
// Ask Claude to summarize latest PR via MCP tools
const prompt =
'Use the MCP tools to get my last pr and describe what it does in 2-3 sentences. Also detail how you collected this information';
const claudeResult = await devbox.cmd.exec(
`ANTHROPIC_API_KEY=${anthropicKey} claude -p "${prompt}" --dangerously-skip-permissions`,
);
const claudeStdout = (await claudeResult.stdout()).trim();
checks.push({
name: 'Claude prompt through MCP succeeds',
passed: claudeResult.exitCode === 0 && claudeStdout.length > 0,
details: claudeResult.exitCode === 0 ? 'non-empty response received' : await claudeResult.stderr(),
});
return { resourcesCreated, checks };
}
function validateEnv(options: McpExampleOptions): { skip: boolean; checks: ExampleCheck[] } {
const checks: ExampleCheck[] = [];
const skipIfMissing = options?.skipIfMissingCredentials === true;
const githubToken = process.env['GITHUB_TOKEN'];
if (!githubToken && skipIfMissing) {
checks.push({ name: 'GITHUB_TOKEN provided', passed: false, details: 'Skipped: missing GITHUB_TOKEN' });
return { skip: true, checks };
}
const anthropicKey = process.env['ANTHROPIC_API_KEY'];
if (!anthropicKey && skipIfMissing) {
checks.push({
name: 'ANTHROPIC_API_KEY provided',
passed: false,
details: 'Skipped: missing ANTHROPIC_API_KEY',
});
return { skip: true, checks };
}
return { skip: false, checks };
}
export const runMcpGithubToolsExample = wrapRecipe({ recipe, validateEnv });
if (require.main === module) {
void runAsCli(runMcpGithubToolsExample);
}