Skip to content

Commit 4fda894

Browse files
committed
feat: add JWT auth
1 parent 94db881 commit 4fda894

File tree

2 files changed

+79
-35
lines changed

2 files changed

+79
-35
lines changed

README.md

Lines changed: 67 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,57 @@
44

55
## Usage
66

7-
### Developer Token Authorization
8-
This MCP server currently supports only Developer Token authentication.
7+
### JSON Web Token (JWT) Authorization (recommended)
8+
9+
Using a JWT Config will allow persistent connection to Box. You will need a paid Box enterprise account, or you can sign up for a [free developer account](https://account.box.com/signup/n/developer) (make sure you are signed out of Box before clicking that link).
10+
11+
Begin by visiting the [Box Developer Console](https://app.box.com/developers/console) and create a new application. Make sure the authorization type is JSON Web Token.
12+
13+
Go to `Configuration > Add and Manage Public Keys` and Generate a Public/Private Keypair.
14+
If you have not already, Box prompt you to set up 2 factor authentication and Authorize the application as an administrator in your box account. You will need to:
15+
16+
1. give the application `App + Enterprise Access`, and
17+
2. enable the `make API calls using the as-user header` option
18+
19+
via the Box Application's Configuration page. **Make sure to reauthorize the application if you are modifying these settings**.
20+
21+
Download/store the configuration JSON somewhere on your computer. We will set the environment variable `BOX_JWT_CONFIG_PATH` to its location. We will also set the `BOX_USER_ID` to the user whose files we will be accessing. You can find your own Box User Id on your [account page](https://app.box.com/account).
22+
23+
#### Claude Desktop Configuration
24+
25+
```json
26+
{
27+
"mcpServers": {
28+
"box": {
29+
"command": "npx",
30+
"args": ["box-mcp-server"],
31+
"env": {
32+
"BOX_JWT_CONFIG_PATH": "/path/to/your/box-jwt-config.json",
33+
"BOX_USER_ID": "123456"
34+
}
35+
}
36+
}
37+
}
38+
```
39+
40+
### Developer Token Authorization (easiest)
41+
42+
Using a developer token is the easiest way to integrate with Box, but will only last 60 minutes.
943

1044
To get started, set the `BOX_DEV_TOKEN` to a [Box Developer Token](https://developer.box.com/guides/authentication/tokens/developer-tokens/).
1145

12-
Begin by visiting the [Box Developer Console](https://app.box.com/developers/console) and create a new application. The authorization type does not currently matter, as all support Box Developer Token.
46+
Begin by visiting the [Box Developer Console](https://app.box.com/developers/console) and create a new application. The authorization type does not currently matter, as all support Box Developer Token.
1347

1448
Once your application is created, navigate to its configuration setings and click `Generate Developer Token`.
1549

16-
### Claude Desktop Configuration
50+
#### Claude Desktop Configuration
51+
1752
```json
1853
{
1954
"mcpServers": {
2055
"box": {
2156
"command": "npx",
22-
"args": [
23-
"box-mcp-server"
24-
],
57+
"args": ["box-mcp-server"],
2558
"env": {
2659
"BOX_DEV_TOKEN": "YOUR_DEV_TOKEN_GOES_HERE"
2760
}
@@ -45,46 +78,52 @@ Once your application is created, navigate to its configuration setings and clic
4578

4679
Before you begin, ensure you have the following installed:
4780

48-
* Node.js (v22 or higher)
49-
* npm
50-
* git
81+
- Node.js (v22 or higher)
82+
- npm
83+
- git
5184

5285
### Setting up Development Environment
5386

5487
To set up the development environment, follow these steps:
5588

5689
1. Fork the repository
57-
* Click the "Fork" button in the top-right corner of this repository
58-
* This creates your own copy of the repository under your Github acocunt
90+
91+
- Click the "Fork" button in the top-right corner of this repository
92+
- This creates your own copy of the repository under your Github acocunt
5993

6094
2. Clone Your Fork:
61-
```sh
62-
git clone https://github.com/YOUR_USERNAME/box-mcp-server.git
63-
cd box-mcp-server
64-
```
95+
96+
```sh
97+
git clone https://github.com/YOUR_USERNAME/box-mcp-server.git
98+
cd box-mcp-server
99+
```
65100

66101
3. Add Upstream Remote
67102
```sh
68103
git remote add upstream https://github.com/hmk/box-mcp-server.git
69104
```
70105
4. Install dependencies:
71-
```sh
72-
npm install
73-
```
106+
107+
```sh
108+
npm install
109+
```
74110

75111
5. Set the `BOX_DEV_TOKEN` environment variable:
76-
```sh
77-
export BOX_DEV_TOKEN=your_developer_token
78-
```
112+
113+
```sh
114+
export BOX_DEV_TOKEN=your_developer_token
115+
```
79116

80117
6. Run watch to keep index.js updated:
81-
```sh
82-
npm run watch
83-
```
118+
119+
```sh
120+
npm run watch
121+
```
84122

85123
7. Start the model context protocol development server:
86-
```sh
87-
npx @modelcontextprotocol/inspector node PATH_TO_YOUR_CLONED_REPO/dist/index.js
88-
```
124+
125+
```sh
126+
npx @modelcontextprotocol/inspector node PATH_TO_YOUR_CLONED_REPO/dist/index.js
127+
```
89128

90129
8. If the development server did not load the environment variable correctly, set the `BOX_DEV_TOKEN` on the left-hand side of the mcp inspector.

src/index.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,22 @@ import {
88
ListToolsRequestSchema,
99
ReadResourceRequestSchema,
1010
} from "@modelcontextprotocol/sdk/types.js";
11-
import { BoxClient, BoxDeveloperTokenAuth } from "box-typescript-sdk-gen";
11+
import { BoxClient, BoxDeveloperTokenAuth, BoxJwtAuth, JwtConfig } from "box-typescript-sdk-gen";
1212
import * as pdfjsLib from "pdfjs-dist/legacy/build/pdf.mjs";
1313
import * as mammoth from "mammoth";
1414

15-
// Initialize the Box client
16-
const boxToken = process.env.BOX_DEV_TOKEN;
17-
if (!boxToken) {
18-
throw new Error("BOX_DEV_TOKEN must be set as an environment variable");
15+
// Initialize Box client based on auth method
16+
let auth: BoxDeveloperTokenAuth | BoxJwtAuth;
17+
if (process.env.BOX_DEV_TOKEN) {
18+
auth = new BoxDeveloperTokenAuth({ token: process.env.BOX_DEV_TOKEN });
19+
} else if (process.env.BOX_JWT_CONFIG_PATH && process.env.BOX_USER_ID) {
20+
const jwtConfig = JwtConfig.fromConfigFile(process.env.BOX_JWT_CONFIG_PATH);
21+
const jwtAuth = new BoxJwtAuth({ config: jwtConfig });
22+
const userAuth = jwtAuth.withUserSubject(process.env.BOX_USER_ID);
23+
auth = userAuth;
24+
} else {
25+
throw new Error("BOX_DEV_TOKEN or BOX_JWT_CONFIG_PATH && BOX_USER_ID must be set as environment variable(s)");
1926
}
20-
21-
const auth = new BoxDeveloperTokenAuth({ token: boxToken });
2227
const client = new BoxClient({ auth });
2328

2429
// Shared file reading functionality

0 commit comments

Comments
 (0)