|
4 | 4 | from pydantic import BaseModel, Field |
5 | 5 | from pydantic_ai.models.openai import OpenAIModel |
6 | 6 | from pydantic_ai.providers.openai import OpenAIProvider |
| 7 | +from mcp_run.client import _convert_type |
7 | 8 |
|
8 | 9 | from typing import TypedDict, List, Set, AsyncIterator, Any |
9 | 10 | import traceback |
10 | 11 |
|
11 | 12 | __all__ = ["BaseModel", "Field", "Agent", "mcp_run", "pydantic_ai", "pydantic"] |
12 | 13 |
|
13 | 14 |
|
14 | | -def _convert_type(t): |
15 | | - if t == "string": |
16 | | - return str |
17 | | - elif t == "boolean": |
18 | | - return bool |
19 | | - elif t == "number": |
20 | | - return float |
21 | | - elif t == "integer": |
22 | | - return int |
23 | | - elif t == "object": |
24 | | - return dict |
25 | | - elif t == "array": |
26 | | - return list |
27 | | - raise TypeError(f"Unhandled conversion type: {t}") |
28 | | - |
29 | | - |
30 | 15 | def openai_compatible_model(url: str, model: str, api_key: str | None = None): |
31 | 16 | """ |
32 | 17 | Returns an OpenAI compatible model from the provided `url`, `model` name and optional `api_key` |
@@ -71,27 +56,20 @@ def register_tool(self, tool: mcp_run.Tool, f=None): |
71 | 56 | return |
72 | 57 |
|
73 | 58 | def wrap(tool, inner): |
74 | | - props = tool.input_schema["properties"] |
75 | | - t = {k: _convert_type(v["type"]) for k, v in props.items()} |
76 | | - InputType = TypedDict("Input", t) |
77 | | - |
78 | 59 | if inner is not None: |
| 60 | + props = tool.input_schema["properties"] |
| 61 | + t = {k: _convert_type(v["type"]) for k, v in props.items()} |
| 62 | + InputType = TypedDict("Input", t) |
79 | 63 |
|
80 | 64 | def f(input: InputType): |
81 | 65 | try: |
82 | 66 | return inner(input) |
83 | 67 | except Exception as exc: |
84 | 68 | return f"ERROR call to tool {tool.name} failed: {traceback.format_exception(exc)}" |
85 | | - else: |
86 | | - |
87 | | - def f(input: InputType): |
88 | | - try: |
89 | | - res = self.client.call_tool(tool=tool.name, params=input) |
90 | | - return res.content[0].text |
91 | | - except Exception as exc: |
92 | | - return f"ERROR call to tool {tool.name} failed: {traceback.format_exception(exc)}" |
93 | 69 |
|
94 | | - return f |
| 70 | + return f |
| 71 | + else: |
| 72 | + return self.client._make_pydantic_function(tool) |
95 | 73 |
|
96 | 74 | self._register_tool( |
97 | 75 | pydantic_ai.Tool( |
|
0 commit comments