Skip to content

Commit c10c090

Browse files
julien-cWauplin
andauthored
[mcp-client] Allow arbitrary endpoint via URL (in both McpClient and Agent) (#1396)
for local AI for instance 🔥 ## To try it Using LM Studio + this model: https://lmstudio.ai/model/qwen2.5-14b-instruct ```sh # in ./packages/mcp-client ENDPOINT_URL=http://localhost:1234/v1 MODEL_ID=qwen2.5-14b-instruct tsx cli.ts ``` --------- Co-authored-by: Lucain <[email protected]>
1 parent 0b8cbb1 commit c10c090

File tree

4 files changed

+59
-21
lines changed

4 files changed

+59
-21
lines changed

packages/inference/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# 🤗 Hugging Face Inference
22

3-
A Typescript powered wrapper for the HF Inference API (serverless), Inference Endpoints (dedicated), and all supported Inference Providers.
4-
It works with [Inference API (serverless)](https://huggingface.co/docs/api-inference/index) and [Inference Endpoints (dedicated)](https://huggingface.co/docs/inference-endpoints/index), and even with all supported third-party Inference Providers.
3+
A Typescript powered wrapper for Inference Providers (serverless) and Inference Endpoints (dedicated).
4+
It works with [Inference Providers (serverless)](https://huggingface.co/docs/api-inference/index) – including all supported third-party Inference Providers – and [Inference Endpoints (dedicated)](https://huggingface.co/docs/inference-endpoints/index), and even with .
55

66
Check out the [full documentation](https://huggingface.co/docs/huggingface.js/inference/README).
77

@@ -25,20 +25,20 @@ yarn add @huggingface/inference
2525

2626
```ts
2727
// esm.sh
28-
import { InferenceClient } from "https://esm.sh/@huggingface/inference"
28+
import { InferenceClient } from "https://esm.sh/@huggingface/inference";
2929
// or npm:
30-
import { InferenceClient } from "npm:@huggingface/inference"
30+
import { InferenceClient } from "npm:@huggingface/inference";
3131
```
3232

3333
### Initialize
3434

3535
```typescript
36-
import { InferenceClient } from '@huggingface/inference'
36+
import { InferenceClient } from '@huggingface/inference';
3737

38-
const hf = new InferenceClient('your access token')
38+
const hf = new InferenceClient('your access token');
3939
```
4040

41-
**Important note:** Using an access token is optional to get started, however you will be rate limited eventually. Join [Hugging Face](https://huggingface.co/join) and then visit [access tokens](https://huggingface.co/settings/tokens) to generate your access token for **free**.
41+
**Important note:** Always pass an access token. Join [Hugging Face](https://huggingface.co/join) and then visit [access tokens](https://huggingface.co/settings/tokens) to generate your access token for **free**.
4242

4343
Your access token should be kept private. If you need to protect it in front-end applications, we suggest setting up a proxy server that stores the access token.
4444

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 BASE_URL = process.env.BASE_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+
BASE_URL
54+
? {
55+
baseUrl: BASE_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)