Skip to content

Commit ded11d7

Browse files
committed
feat: add client type identification to API requests
1 parent 36acea7 commit ded11d7

File tree

7 files changed

+58
-6
lines changed

7 files changed

+58
-6
lines changed

manifest.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"command": "node",
3737
"args": ["${__dirname}/build/index.js"],
3838
"env": {
39-
"PAGEINDEX_API_KEY": "${user_config.api_key}"
39+
"PAGEINDEX_API_KEY": "${user_config.api_key}",
40+
"PAGEINDEX_API_URL": "${user_config.api_url}"
4041
}
4142
}
4243
},
@@ -46,6 +47,13 @@
4647
"title": "PageIndex API Key",
4748
"description": "Your PageIndex API key. Get one at https://dash.pageindex.ai/api-keys",
4849
"required": true
50+
},
51+
"api_url": {
52+
"type": "string",
53+
"title": "PageIndex API URL",
54+
"description": "PageIndex API base URL (optional, defaults to https://dash.pageindex.ai)",
55+
"required": false,
56+
"default": "https://dash.pageindex.ai"
4957
}
5058
}
5159
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"format": "biome format --write .",
1717
"clean": "rm -rf build",
1818
"prepublishOnly": "npm run clean && npm run build",
19-
"dxt:build": "tsup",
19+
"dxt:build": "CLIENT_TYPE=dxt tsup",
2020
"dxt:pack": "npm run dxt:build && dxt pack",
2121
"dxt:dev": "dxt dev",
2222
"postinstall": "node -e \"try { require('fs').existsSync('.git') && require('simple-git-hooks')() } catch(e) {}\"",

src/client/auth.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@ import type { FetchLike } from '@modelcontextprotocol/sdk/shared/transport.js';
44
* Creates an authenticated fetch function that adds Bearer token authentication
55
* This is a simplified approach compared to implementing the complex OAuth interface
66
*/
7-
export function createAuthenticatedFetch(apiKey: string): FetchLike {
7+
export function createAuthenticatedFetch(
8+
apiKey: string,
9+
additionalHeaders: Record<string, string> = {},
10+
): FetchLike {
811
return async (input: string | URL, init?: RequestInit): Promise<Response> => {
912
const headers = new Headers(init?.headers);
1013
headers.set('Authorization', `Bearer ${apiKey}`);
1114

15+
// Add additional headers (like client identification)
16+
Object.entries(additionalHeaders).forEach(([key, value]) => {
17+
headers.set(key, value);
18+
});
19+
1220
return fetch(input, {
1321
...init,
1422
headers,

src/client/mcp-client.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ export class PageIndexMcpClient {
2020
this.apiKey = apiKey;
2121
}
2222

23+
/**
24+
* Get client identification headers
25+
*/
26+
private getClientHeaders(): Record<string, string> {
27+
return {
28+
'X-Client-Type': __CLIENT_TYPE__,
29+
'X-Client-Version': __VERSION__,
30+
};
31+
}
32+
2333
/**
2434
* Connect to the remote PageIndex MCP server
2535
*/
@@ -43,8 +53,12 @@ export class PageIndexMcpClient {
4353

4454
// Try StreamableHTTP first, fallback to SSE for compatibility
4555
try {
46-
// Use simplified authenticated fetch
47-
const authenticatedFetch = createAuthenticatedFetch(this.apiKey);
56+
// Use simplified authenticated fetch with client headers
57+
const clientHeaders = this.getClientHeaders();
58+
const authenticatedFetch = createAuthenticatedFetch(
59+
this.apiKey,
60+
clientHeaders,
61+
);
4862

4963
this.transport = new StreamableHTTPClientTransport(
5064
streamableHttpUrl,
@@ -61,7 +75,11 @@ export class PageIndexMcpClient {
6175
} catch (_error) {
6276
try {
6377
// For SSE transport, we need to pass authenticated fetch as well
64-
const authenticatedFetch = createAuthenticatedFetch(this.apiKey);
78+
const clientHeaders = this.getClientHeaders();
79+
const authenticatedFetch = createAuthenticatedFetch(
80+
this.apiKey,
81+
clientHeaders,
82+
);
6583
this.transport = new SSEClientTransport(sseUrl, {
6684
fetch: authenticatedFetch,
6785
});

src/globals.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
declare const __VERSION__: string;
2+
declare const __CLIENT_TYPE__: 'dxt' | 'npm';

src/tools/remote-proxy.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ export class RemoteToolsProxy {
1515
this.client = client;
1616
}
1717

18+
/**
19+
* Get client type information
20+
*/
21+
getClientInfo(): {
22+
type: 'dxt' | 'npm';
23+
version: string;
24+
isDxt: boolean;
25+
} {
26+
return {
27+
type: __CLIENT_TYPE__,
28+
version: __VERSION__,
29+
isDxt: __CLIENT_TYPE__ === 'dxt',
30+
};
31+
}
32+
1833
/**
1934
* Fetch tool definitions from remote MCP server
2035
*/

tsup.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default defineConfig({
1414
noExternal: [/.*/], // Bundle all dependencies
1515
define: {
1616
__VERSION__: `"${packageJson.version}"`,
17+
__CLIENT_TYPE__: `"${process.env.CLIENT_TYPE || 'npm'}"`,
1718
},
1819
platform: 'node',
1920
onSuccess: async () => {

0 commit comments

Comments
 (0)