Skip to content

Commit a5e089c

Browse files
julien-cmishig25Wauplin
authored
PoC: InferenceClient is also a MCPClient (#2986)
* Add extra dependency * PoC: `InferenceClient` is also a `MCPClient` * [using Claude] change the code to make MCPClient inherit from AsyncInferenceClient * Update mcp_client.py Co-authored-by: Mishig <dmishig@gmail.com> * mcp_client: Support multiple servers (#2987) cc @mishig25 * Revert "[using Claude] change the code to make MCPClient inherit from AsyncInferenceClient" This reverts commit 2c7329c. * `add_mcp_server`: the env should not be hardcoded here cc @Wauplin * Handle the "no tool call" case * Update setup.py * Async mcp client + example + code quality * docstring * accept ChatCompletionInputMessage as input * lazy loading * style * better type * no need mcp for dev * code quality on Python 3.8 * address feedback * address feedback * do not close client inside of �[200~process_single_turn_with_tools~ * docstring, no more warning, garbage collection * docs --------- Co-authored-by: Mishig <dmishig@gmail.com> Co-authored-by: Lucain Pouget <lucainp@gmail.com> Co-authored-by: Lucain <lucain@huggingface.co>
1 parent abec974 commit a5e089c

File tree

7 files changed

+340
-12
lines changed

7 files changed

+340
-12
lines changed

setup.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ def get_version() -> str:
6464

6565
extras["hf_xet"] = ["hf-xet>=1.1.1,<2.0.0"]
6666

67+
extras["mcp"] = [
68+
"mcp>=1.8.0",
69+
] + extras["inference"]
70+
6771
extras["testing"] = (
6872
extras["cli"]
6973
+ extras["inference"]

src/huggingface_hub/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,9 @@
440440
"ZeroShotObjectDetectionOutputElement",
441441
"ZeroShotObjectDetectionParameters",
442442
],
443+
"inference._mcp.mcp_client": [
444+
"MCPClient",
445+
],
443446
"inference_api": [
444447
"InferenceApi",
445448
],
@@ -644,6 +647,7 @@
644647
"InferenceEndpointType",
645648
"InferenceTimeoutError",
646649
"KerasModelHubMixin",
650+
"MCPClient",
647651
"ModelCard",
648652
"ModelCardData",
649653
"ModelHubMixin",
@@ -1402,6 +1406,7 @@ def __dir__():
14021406
ZeroShotObjectDetectionOutputElement, # noqa: F401
14031407
ZeroShotObjectDetectionParameters, # noqa: F401
14041408
)
1409+
from .inference._mcp.mcp_client import MCPClient # noqa: F401
14051410
from .inference_api import InferenceApi # noqa: F401
14061411
from .keras_mixin import (
14071412
KerasModelHubMixin, # noqa: F401

src/huggingface_hub/inference/_client.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
AudioToAudioOutputElement,
6767
AutomaticSpeechRecognitionOutput,
6868
ChatCompletionInputGrammarType,
69+
ChatCompletionInputMessage,
6970
ChatCompletionInputStreamOptions,
7071
ChatCompletionInputTool,
7172
ChatCompletionInputToolChoiceClass,
@@ -100,7 +101,7 @@
100101
ZeroShotClassificationOutputElement,
101102
ZeroShotImageClassificationOutputElement,
102103
)
103-
from huggingface_hub.inference._providers import PROVIDER_T, get_provider_helper
104+
from huggingface_hub.inference._providers import PROVIDER_OR_POLICY_T, get_provider_helper
104105
from huggingface_hub.utils import build_hf_headers, get_session, hf_raise_for_status
105106
from huggingface_hub.utils._auth import get_token
106107
from huggingface_hub.utils._deprecation import _deprecate_method
@@ -164,7 +165,7 @@ def __init__(
164165
self,
165166
model: Optional[str] = None,
166167
*,
167-
provider: Union[Literal["auto"], PROVIDER_T, None] = None,
168+
provider: Optional[PROVIDER_OR_POLICY_T] = None,
168169
token: Optional[str] = None,
169170
timeout: Optional[float] = None,
170171
headers: Optional[Dict[str, str]] = None,
@@ -446,7 +447,7 @@ def automatic_speech_recognition(
446447
@overload
447448
def chat_completion( # type: ignore
448449
self,
449-
messages: List[Dict],
450+
messages: List[Union[Dict, ChatCompletionInputMessage]],
450451
*,
451452
model: Optional[str] = None,
452453
stream: Literal[False] = False,
@@ -472,7 +473,7 @@ def chat_completion( # type: ignore
472473
@overload
473474
def chat_completion( # type: ignore
474475
self,
475-
messages: List[Dict],
476+
messages: List[Union[Dict, ChatCompletionInputMessage]],
476477
*,
477478
model: Optional[str] = None,
478479
stream: Literal[True] = True,
@@ -498,7 +499,7 @@ def chat_completion( # type: ignore
498499
@overload
499500
def chat_completion(
500501
self,
501-
messages: List[Dict],
502+
messages: List[Union[Dict, ChatCompletionInputMessage]],
502503
*,
503504
model: Optional[str] = None,
504505
stream: bool = False,
@@ -523,7 +524,7 @@ def chat_completion(
523524

524525
def chat_completion(
525526
self,
526-
messages: List[Dict],
527+
messages: List[Union[Dict, ChatCompletionInputMessage]],
527528
*,
528529
model: Optional[str] = None,
529530
stream: bool = False,

src/huggingface_hub/inference/_generated/_async_client.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
AudioToAudioOutputElement,
5252
AutomaticSpeechRecognitionOutput,
5353
ChatCompletionInputGrammarType,
54+
ChatCompletionInputMessage,
5455
ChatCompletionInputStreamOptions,
5556
ChatCompletionInputTool,
5657
ChatCompletionInputToolChoiceClass,
@@ -85,7 +86,7 @@
8586
ZeroShotClassificationOutputElement,
8687
ZeroShotImageClassificationOutputElement,
8788
)
88-
from huggingface_hub.inference._providers import PROVIDER_T, get_provider_helper
89+
from huggingface_hub.inference._providers import PROVIDER_OR_POLICY_T, get_provider_helper
8990
from huggingface_hub.utils import build_hf_headers, get_session, hf_raise_for_status
9091
from huggingface_hub.utils._auth import get_token
9192
from huggingface_hub.utils._deprecation import _deprecate_method
@@ -154,7 +155,7 @@ def __init__(
154155
self,
155156
model: Optional[str] = None,
156157
*,
157-
provider: Union[Literal["auto"], PROVIDER_T, None] = None,
158+
provider: Optional[PROVIDER_OR_POLICY_T] = None,
158159
token: Optional[str] = None,
159160
timeout: Optional[float] = None,
160161
headers: Optional[Dict[str, str]] = None,
@@ -480,7 +481,7 @@ async def automatic_speech_recognition(
480481
@overload
481482
async def chat_completion( # type: ignore
482483
self,
483-
messages: List[Dict],
484+
messages: List[Union[Dict, ChatCompletionInputMessage]],
484485
*,
485486
model: Optional[str] = None,
486487
stream: Literal[False] = False,
@@ -506,7 +507,7 @@ async def chat_completion( # type: ignore
506507
@overload
507508
async def chat_completion( # type: ignore
508509
self,
509-
messages: List[Dict],
510+
messages: List[Union[Dict, ChatCompletionInputMessage]],
510511
*,
511512
model: Optional[str] = None,
512513
stream: Literal[True] = True,
@@ -532,7 +533,7 @@ async def chat_completion( # type: ignore
532533
@overload
533534
async def chat_completion(
534535
self,
535-
messages: List[Dict],
536+
messages: List[Union[Dict, ChatCompletionInputMessage]],
536537
*,
537538
model: Optional[str] = None,
538539
stream: bool = False,
@@ -557,7 +558,7 @@ async def chat_completion(
557558

558559
async def chat_completion(
559560
self,
560-
messages: List[Dict],
561+
messages: List[Union[Dict, ChatCompletionInputMessage]],
561562
*,
562563
model: Optional[str] = None,
563564
stream: bool = False,

src/huggingface_hub/inference/_mcp/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)