Skip to content

Commit c801d9e

Browse files
authored
[feat] allowing a custom base url through call
1 parent 7f47326 commit c801d9e

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
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';

0 commit comments

Comments
 (0)