Skip to content

Commit dfd8e7e

Browse files
committed
refactor: rename endpoint tool to test_request and improve build process
- Rename endpoint tool to test_request for better clarity - Add dynamic response size limit information in tool description - Remove redundant postbuild script - Update documentation and examples with proper MCP tool usage
1 parent a20cf35 commit dfd8e7e

File tree

5 files changed

+122
-46
lines changed

5 files changed

+122
-46
lines changed

CLINE-CUSTOM-INSTRUCTIONS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# REST API Testing Instructions
22

3-
Use this tool when testing, debugging, or interacting with REST API endpoints. The tool provides comprehensive request/response information and handles authentication automatically.
3+
The `test_request` tool enables testing, debugging, and interacting with REST API endpoints. The tool provides comprehensive request/response information and handles authentication automatically.
44

55
## When to Use
66

README.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ Add to `C:\Users\<YourUsername>\AppData\Roaming\Code\User\globalStorage\saoudriz
5252
"AUTH_APIKEY_HEADER_NAME": "X-API-Key",
5353
"AUTH_APIKEY_VALUE": "your-api-key",
5454
// SSL Verification (enabled by default)
55-
"REST_ENABLE_SSL_VERIFY": "false" // Set to false to disable SSL verification for self-signed certificates
55+
"REST_ENABLE_SSL_VERIFY": "false", // Set to false to disable SSL verification for self-signed certificates
56+
// Response Size Limit (optional, defaults to 10000 bytes)
57+
"REST_RESPONSE_SIZE_LIMIT": "10000" // Maximum response size in bytes
5658
}
5759
}
5860
}
@@ -100,6 +102,12 @@ Note: Replace the environment variables with your actual values. Only configure
100102
- Detailed response information including status, headers, and body
101103
- Custom header support
102104
- Request body handling for POST/PUT methods
105+
- Response Size Management:
106+
- Automatic response size limiting (default: 10KB/10000 bytes)
107+
- Configurable size limit via REST_RESPONSE_SIZE_LIMIT environment variable
108+
- Clear truncation metadata when responses exceed limit
109+
- Preserves response structure while only truncating body content
110+
103111
- SSL Certificate Verification:
104112
- Enabled by default for secure operation
105113
- Can be disabled for self-signed certificates or development environments
@@ -115,30 +123,30 @@ Once installed and configured, you can use the REST API Tester through Cline to
115123

116124
```typescript
117125
// Test a GET endpoint
118-
{
126+
use_mcp_tool('rest-api', 'test_request', {
119127
"method": "GET",
120128
"endpoint": "/users"
121-
}
129+
});
122130

123131
// Test a POST endpoint with body
124-
{
132+
use_mcp_tool('rest-api', 'test_request', {
125133
"method": "POST",
126134
"endpoint": "/users",
127135
"body": {
128136
"name": "John Doe",
129137
"email": "john@example.com"
130138
}
131-
}
139+
});
132140

133141
// Test with custom headers
134-
{
142+
use_mcp_tool('rest-api', 'test_request', {
135143
"method": "GET",
136144
"endpoint": "/products",
137145
"headers": {
138146
"Accept-Language": "en-US",
139147
"X-Custom-Header": "custom-value"
140148
}
141-
}
149+
});
142150
```
143151

144152
## Development

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"scripts": {
1717
"prebuild": "node scripts/build.js",
1818
"build": "tsc",
19-
"postbuild": "node scripts/build.js",
2019
"prepare": "npm run build",
2120
"watch": "tsc --watch",
2221
"inspector": "npx @modelcontextprotocol/inspector build/index.js"

src/index.ts

Lines changed: 94 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,20 @@ import {
1010
McpError,
1111
} from '@modelcontextprotocol/sdk/types.js';
1212
import axios, { AxiosInstance, AxiosRequestConfig, Method } from 'axios';
13-
import { VERSION, PACKAGE_NAME, SERVER_NAME } from './version.js';
13+
import { VERSION, SERVER_NAME } from './version.js';
1414

1515
if (!process.env.REST_BASE_URL) {
1616
throw new Error('REST_BASE_URL environment variable is required');
1717
}
18+
19+
// Default response size limit: 10KB (10000 bytes)
20+
const RESPONSE_SIZE_LIMIT = process.env.REST_RESPONSE_SIZE_LIMIT
21+
? parseInt(process.env.REST_RESPONSE_SIZE_LIMIT, 10)
22+
: 10000;
23+
24+
if (isNaN(RESPONSE_SIZE_LIMIT) || RESPONSE_SIZE_LIMIT <= 0) {
25+
throw new Error('REST_RESPONSE_SIZE_LIMIT must be a positive number');
26+
}
1827
const AUTH_BASIC_USERNAME = process.env.AUTH_BASIC_USERNAME;
1928
const AUTH_BASIC_PASSWORD = process.env.AUTH_BASIC_PASSWORD;
2029
const AUTH_BEARER = process.env.AUTH_BEARER;
@@ -29,6 +38,35 @@ interface EndpointArgs {
2938
headers?: Record<string, string>;
3039
}
3140

41+
interface ValidationResult {
42+
isError: boolean;
43+
messages: string[];
44+
truncated?: {
45+
originalSize: number;
46+
returnedSize: number;
47+
truncationPoint: number;
48+
sizeLimit: number;
49+
};
50+
}
51+
52+
interface ResponseObject {
53+
request: {
54+
url: string;
55+
method: string;
56+
headers: Record<string, string | undefined>;
57+
body: any;
58+
authMethod: string;
59+
};
60+
response: {
61+
statusCode: number;
62+
statusText: string;
63+
timing: string;
64+
headers: Record<string, any>;
65+
body: any;
66+
};
67+
validation: ValidationResult;
68+
}
69+
3270
const isValidEndpointArgs = (args: any): args is EndpointArgs => {
3371
if (typeof args !== 'object' || args === null) return false;
3472
if (!['GET', 'POST', 'PUT', 'DELETE'].includes(args.method)) return false;
@@ -145,7 +183,7 @@ class RestTester {
145183
this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
146184
tools: [
147185
{
148-
name: 'endpoint',
186+
name: 'test_request',
149187
description: `Test a REST API endpoint and get detailed response information.
150188
151189
Base URL: ${process.env.REST_BASE_URL}
@@ -166,6 +204,7 @@ The tool automatically:
166204
- Normalizes endpoints (adds leading slash, removes trailing slashes)
167205
- Handles authentication header injection
168206
- Accepts any HTTP status code as valid
207+
- Limits response size to ${RESPONSE_SIZE_LIMIT} bytes (configurable via REST_RESPONSE_SIZE_LIMIT)
169208
- Returns detailed response information including:
170209
* Full URL called
171210
* Status code and text
@@ -211,7 +250,7 @@ Error Handling:
211250
}));
212251

213252
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
214-
if (request.params.name !== 'endpoint') {
253+
if (request.params.name !== 'test_request') {
215254
throw new McpError(
216255
ErrorCode.MethodNotFound,
217256
`Unknown tool: ${request.params.name}`
@@ -271,32 +310,62 @@ Error Handling:
271310
else if (hasBearerAuth()) authMethod = 'bearer';
272311
else if (hasApiKeyAuth()) authMethod = 'apikey';
273312

313+
// Prepare response object
314+
const responseObj: ResponseObject = {
315+
request: {
316+
url: fullUrl,
317+
method: config.method || 'GET',
318+
headers: config.headers as Record<string, string | undefined>,
319+
body: config.data,
320+
authMethod
321+
},
322+
response: {
323+
statusCode: response.status,
324+
statusText: response.statusText,
325+
timing: `${endTime - startTime}ms`,
326+
headers: response.headers as Record<string, any>,
327+
body: response.data,
328+
},
329+
validation: {
330+
isError: response.status >= 400,
331+
messages: response.status >= 400 ?
332+
[`Request failed with status ${response.status}`] :
333+
['Request completed successfully']
334+
}
335+
};
336+
337+
// Check response size
338+
const stringified = JSON.stringify(responseObj, null, 2);
339+
const totalBytes = Buffer.from(stringified).length;
340+
341+
if (totalBytes > RESPONSE_SIZE_LIMIT) {
342+
// Convert body to string if it isn't already
343+
const bodyStr = typeof response.data === 'string'
344+
? response.data
345+
: JSON.stringify(response.data);
346+
347+
// Calculate how much we need to truncate
348+
const currentSize = Buffer.from(bodyStr).length;
349+
const targetSize = Math.max(0, currentSize - (totalBytes - RESPONSE_SIZE_LIMIT));
350+
351+
// Create truncated response
352+
responseObj.response.body = bodyStr.slice(0, targetSize);
353+
responseObj.validation.messages.push(
354+
`Response truncated: ${targetSize} of ${currentSize} bytes returned due to size limit (${RESPONSE_SIZE_LIMIT} bytes)`
355+
);
356+
responseObj.validation.truncated = {
357+
originalSize: currentSize,
358+
returnedSize: targetSize,
359+
truncationPoint: targetSize,
360+
sizeLimit: RESPONSE_SIZE_LIMIT
361+
};
362+
}
363+
274364
return {
275365
content: [
276366
{
277367
type: 'text',
278-
text: JSON.stringify({
279-
request: {
280-
url: fullUrl,
281-
method: config.method,
282-
headers: config.headers,
283-
body: config.data,
284-
authMethod
285-
},
286-
response: {
287-
statusCode: response.status,
288-
statusText: response.statusText,
289-
timing: `${endTime - startTime}ms`,
290-
headers: response.headers,
291-
body: response.data,
292-
},
293-
validation: {
294-
isError: response.status >= 400,
295-
messages: response.status >= 400 ?
296-
[`Request failed with status ${response.status}`] :
297-
['Request completed successfully']
298-
}
299-
}, null, 2),
368+
text: JSON.stringify(responseObj, null, 2),
300369
},
301370
],
302371
};

src/resources/examples.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,59 @@
22

33
## Basic GET Request
44
```typescript
5-
{
5+
use_mcp_tool('rest-api', 'test_request', {
66
"method": "GET",
77
"endpoint": "/users"
8-
}
8+
});
99
```
1010

1111
## GET with Query Parameters
1212
```typescript
13-
{
13+
use_mcp_tool('rest-api', 'test_request', {
1414
"method": "GET",
1515
"endpoint": "/users?role=admin&status=active"
16-
}
16+
});
1717
```
1818

1919
## POST Request with Body
2020
```typescript
21-
{
21+
use_mcp_tool('rest-api', 'test_request', {
2222
"method": "POST",
2323
"endpoint": "/users",
2424
"body": {
2525
"name": "John Doe",
2626
"email": "john@example.com"
2727
}
28-
}
28+
});
2929
```
3030

3131
## Request with Custom Headers
3232
```typescript
33-
{
33+
use_mcp_tool('rest-api', 'test_request', {
3434
"method": "GET",
3535
"endpoint": "/secure-resource",
3636
"headers": {
3737
"Custom-Header": "value",
3838
"Another-Header": "another-value"
3939
}
40-
}
40+
});
4141
```
4242

4343
## PUT Request Example
4444
```typescript
45-
{
45+
use_mcp_tool('rest-api', 'test_request', {
4646
"method": "PUT",
4747
"endpoint": "/users/123",
4848
"body": {
4949
"name": "Updated Name",
5050
"status": "inactive"
5151
}
52-
}
52+
});
5353
```
5454

5555
## DELETE Request Example
5656
```typescript
57-
{
57+
use_mcp_tool('rest-api', 'test_request', {
5858
"method": "DELETE",
5959
"endpoint": "/users/123"
60-
}
60+
});

0 commit comments

Comments
 (0)