Skip to content

Commit 21ae7c7

Browse files
committed
Support arbitrary (local or remote) endpoint
1 parent f40e1f9 commit 21ae7c7

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

packages/mcp-client/cli.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { version as packageVersion } from "./package.json";
1111

1212
const MODEL_ID = process.env.MODEL_ID ?? "Qwen/Qwen2.5-72B-Instruct";
1313
const PROVIDER = (process.env.PROVIDER as InferenceProvider) ?? "nebius";
14+
const ENDPOINT_URL = process.env.ENDPOINT_URL;
1415

1516
const SERVERS: StdioServerParameters[] = [
1617
{
@@ -48,12 +49,21 @@ async function main() {
4849
process.exit(1);
4950
}
5051

51-
const agent = new Agent({
52-
provider: PROVIDER,
53-
model: MODEL_ID,
54-
apiKey: process.env.HF_TOKEN,
55-
servers: SERVERS,
56-
});
52+
const agent = new Agent(
53+
ENDPOINT_URL
54+
? {
55+
baseUrl: ENDPOINT_URL,
56+
model: MODEL_ID,
57+
apiKey: process.env.HF_TOKEN,
58+
servers: SERVERS,
59+
}
60+
: {
61+
provider: PROVIDER,
62+
model: MODEL_ID,
63+
apiKey: process.env.HF_TOKEN,
64+
servers: SERVERS,
65+
}
66+
);
5767

5868
const rl = readline.createInterface({ input: stdin, output: stdout });
5969
let abortController = new AbortController();

packages/mcp-client/src/Agent.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,28 @@ export class Agent extends McpClient {
4949

5050
constructor({
5151
provider,
52+
baseUrl,
5253
model,
5354
apiKey,
5455
servers,
5556
prompt,
56-
}: {
57-
provider: InferenceProvider;
57+
}: (
58+
| {
59+
provider: InferenceProvider;
60+
baseUrl?: undefined;
61+
}
62+
| {
63+
baseUrl: string;
64+
provider?: undefined;
65+
}
66+
) & {
5867
model: string;
5968
apiKey: string;
6069
servers: StdioServerParameters[];
6170
prompt?: string;
6271
}) {
63-
super({ provider, model, apiKey });
72+
super(provider ? { provider, baseUrl, model, apiKey } : { provider, baseUrl, model, apiKey });
73+
/// ^This shenanigan is just here to please an overzealous TS type-checker.
6474
this.servers = servers;
6575
this.messages = [
6676
{

packages/mcp-client/src/McpClient.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
22
import type { StdioServerParameters } from "@modelcontextprotocol/sdk/client/stdio.js";
33
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
44
import { InferenceClient } from "@huggingface/inference";
5-
import type { InferenceProvider } from "@huggingface/inference";
5+
import type { InferenceClientEndpoint, InferenceProvider } from "@huggingface/inference";
66
import type {
77
ChatCompletionInputMessage,
88
ChatCompletionInputTool,
@@ -22,14 +22,32 @@ export interface ChatCompletionInputMessageTool extends ChatCompletionInputMessa
2222
}
2323

2424
export class McpClient {
25-
protected client: InferenceClient;
26-
protected provider: string;
25+
protected client: InferenceClient | InferenceClientEndpoint;
26+
protected provider: string | undefined;
27+
2728
protected model: string;
2829
private clients: Map<ToolName, Client> = new Map();
2930
public readonly availableTools: ChatCompletionInputTool[] = [];
3031

31-
constructor({ provider, model, apiKey }: { provider: InferenceProvider; model: string; apiKey: string }) {
32-
this.client = new InferenceClient(apiKey);
32+
constructor({
33+
provider,
34+
baseUrl,
35+
model,
36+
apiKey,
37+
}: (
38+
| {
39+
provider: InferenceProvider;
40+
baseUrl?: undefined;
41+
}
42+
| {
43+
baseUrl: string;
44+
provider?: undefined;
45+
}
46+
) & {
47+
model: string;
48+
apiKey: string;
49+
}) {
50+
this.client = baseUrl ? new InferenceClient(apiKey).endpoint(baseUrl) : new InferenceClient(apiKey);
3351
this.provider = provider;
3452
this.model = model;
3553
}

0 commit comments

Comments
 (0)