Skip to content

Commit 9c81e5a

Browse files
authored
Merge pull request #11 from IVainqueur/main
feat: add support for per-request host override in REST API tester
2 parents 7f47326 + 9ea6ca9 commit 9c81e5a

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

src/index.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ interface EndpointArgs {
3636
endpoint: string;
3737
body?: any;
3838
headers?: Record<string, string>;
39+
host?: string;
3940
}
4041

4142
interface ValidationResult {
@@ -133,6 +134,24 @@ const isValidEndpointArgs = (args: any): args is EndpointArgs => {
133134
`To test a different base URL, update the REST_BASE_URL environment variable.`
134135
);
135136
}
137+
// Validate .host if present
138+
if (args.host !== undefined) {
139+
try {
140+
const url = new URL(args.host);
141+
if (!/^https?:$/.test(url.protocol)) {
142+
throw new Error();
143+
}
144+
// Remove trailing slash if present
145+
if (url.pathname.endsWith('/') && url.pathname !== '/') {
146+
url.pathname = url.pathname.replace(/\/+$/, '');
147+
args.host = url.origin + url.pathname;
148+
} else {
149+
args.host = url.origin + url.pathname;
150+
}
151+
} catch (e) {
152+
throw new McpError(ErrorCode.InvalidParams, `Invalid host format. The 'host' argument must be a valid URL starting with http:// or https://, e.g. "https://example.com" or "http://localhost:3001/api/v1". Received: "${args.host}"`);
153+
}
154+
}
136155

137156
return true;
138157
};
@@ -353,10 +372,11 @@ class RestTester {
353372
// Ensure endpoint starts with / and remove any trailing slashes
354373
const normalizedEndpoint = `/${request.params.arguments.endpoint.replace(/^\/+|\/+$/g, '')}`;
355374

375+
const fullUrl = `${request.params.arguments.host || process.env.REST_BASE_URL}${normalizedEndpoint}`;
356376
// Initialize request config
357377
const config: AxiosRequestConfig = {
358378
method: request.params.arguments.method as Method,
359-
url: normalizedEndpoint,
379+
url: fullUrl,
360380
headers: {},
361381
};
362382

@@ -398,7 +418,6 @@ class RestTester {
398418
const startTime = Date.now();
399419
const response = await this.axiosInstance.request(config);
400420
const endTime = Date.now();
401-
const fullUrl = `${process.env.REST_BASE_URL}${normalizedEndpoint}`;
402421

403422
// Determine auth method used
404423
let authMethod = 'none';

src/resources/examples.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
# REST API Testing Examples
22

3-
⚠️ IMPORTANT: Only provide the endpoint path - do not include full URLs. Your path will be automatically resolved to the full URL.
3+
⚠️ IMPORTANT:
4+
- Only provide the endpoint path in the `endpoint` argument—do not include full URLs. Your path will be automatically resolved to the full URL using the configured base URL or the optional `host` argument.
5+
- To override the base URL for a single request, use the optional `host` argument. This must be a valid URL starting with `http://` or `https://`, and may include a path (trailing slashes will be removed).
46

57
For example, if the base URL is `http://localhost:3000`:
68
✅ Correct: `"/api/users"` → Resolves to: `http://localhost:3000/api/users`
79
❌ Incorrect: `"http://localhost:3000/api/users"` or `"www.example.com/api/users"`
810

11+
If you use a `host` argument:
12+
✅ Correct: `"host": "https://api.example.com/v1", "endpoint": "/users"` → Resolves to: `https://api.example.com/v1/users`
13+
914
## Basic GET Request
1015
```typescript
1116
use_mcp_tool('rest-api', 'test_request', {
1217
"method": "GET",
13-
"endpoint": "/users" // Will be appended to REST_BASE_URL
18+
"endpoint": "/users" // Will be appended to REST_BASE_URL or 'host' if provided
1419
});
1520
```
1621

1722
## GET with Query Parameters
1823
```typescript
1924
use_mcp_tool('rest-api', 'test_request', {
2025
"method": "GET",
21-
"endpoint": "/users?role=admin&status=active"
26+
"endpoint": "/users?role=admin&status=active" // Always a path, not a full URL
2227
});
2328
```
2429

@@ -66,8 +71,24 @@ use_mcp_tool('rest-api', 'test_request', {
6671
});
6772
```
6873

74+
## Using the Optional `host` Argument
75+
You can override the default base URL for a single request by providing a `host` argument. This must be a valid URL starting with `http://` or `https://`, and may include a path (trailing slashes will be removed).
76+
77+
```typescript
78+
use_mcp_tool('rest-api', 'test_request', {
79+
"method": "GET",
80+
"endpoint": "/users",
81+
"host": "https://api.example.com/v1" // The request will go to https://api.example.com/v1/users
82+
});
83+
```
84+
85+
- The `host` argument must include the protocol (http or https).
86+
- If a path is included, any trailing slash will be removed.
87+
- If `host` is invalid, you will receive a clear error message.
88+
- The `endpoint` argument must always be a path, never a full URL.
89+
6990
## Changing Base URL
70-
If you need to test against a different base URL, update the base URL configuration rather than including the full URL in the endpoint parameter.
91+
If you need to test against a different base URL for all requests, update the base URL configuration rather than including the full URL in the endpoint parameter. For a single request, use the `host` argument as shown above.
7192

7293
Example:
7394
```bash
@@ -78,3 +99,6 @@ Example:
7899
# 1. Update the base URL configuration to: https://api.example.com
79100
# 2. Then use just the path:
80101
"endpoint": "/users" # This will resolve to: https://api.example.com/users
102+
# Or, for a single request:
103+
"host": "https://api.example.com", "endpoint": "/users" # This will resolve to: https://api.example.com/users
104+
```

0 commit comments

Comments
 (0)