|
22 | 22 | logger = logging.getLogger(__name__)
|
23 | 23 |
|
24 | 24 |
|
25 |
| -def jsonschema2pydantic(json_schema: str, output_path: Path) -> Type[BaseModel]: |
26 |
| - """Generate a Pydantic model from a JSON schema. |
27 |
| -
|
28 |
| - Parameters |
29 |
| - ---------- |
30 |
| - json_schema : str |
31 |
| - The JSON schema as a string |
32 |
| - output_path : Path |
33 |
| - Path where to save the generated model |
34 |
| -
|
35 |
| - Returns |
36 |
| - ------- |
37 |
| - Type[BaseModel] |
38 |
| - The generated Pydantic model class |
39 |
| - """ |
40 |
| - generate( |
41 |
| - json_schema, |
42 |
| - input_file_type=InputFileType.JsonSchema, |
43 |
| - input_filename="schema.json", |
44 |
| - output=output_path, |
45 |
| - output_model_type=DataModelType.PydanticV2BaseModel, |
46 |
| - enum_field_as_literal=LiteralType.All, |
47 |
| - use_annotated=True, |
48 |
| - reuse_model=True, |
49 |
| - field_constraints=True, |
50 |
| - ) |
51 |
| - |
52 |
| - # Load the generated module |
53 |
| - spec = importlib.util.spec_from_file_location("generated_model", output_path) |
54 |
| - module = importlib.util.module_from_spec(spec) |
55 |
| - sys.modules["generated_model"] = module |
56 |
| - spec.loader.exec_module(module) |
57 |
| - |
58 |
| - # Get the generated model class |
59 |
| - return getattr(module, "Model") |
60 |
| - |
61 |
| - |
62 | 25 | class MCPServerConfig(BaseModel):
|
63 | 26 | command: str
|
64 | 27 | args: list[str] | None = None
|
@@ -148,6 +111,43 @@ async def cleanup(self) -> None:
|
148 | 111 | # await stack.aclose()
|
149 | 112 |
|
150 | 113 |
|
| 114 | +def jsonschema2pydantic(json_schema: str, output_path: Path) -> Type[BaseModel]: |
| 115 | + """Generate a Pydantic model from a JSON schema. |
| 116 | +
|
| 117 | + Parameters |
| 118 | + ---------- |
| 119 | + json_schema : str |
| 120 | + The JSON schema as a string |
| 121 | + output_path : Path |
| 122 | + Path where to save the generated model |
| 123 | +
|
| 124 | + Returns |
| 125 | + ------- |
| 126 | + Type[BaseModel] |
| 127 | + The generated Pydantic model class |
| 128 | + """ |
| 129 | + generate( |
| 130 | + json_schema, |
| 131 | + input_file_type=InputFileType.JsonSchema, |
| 132 | + input_filename="schema.json", |
| 133 | + output=output_path, |
| 134 | + output_model_type=DataModelType.PydanticV2BaseModel, |
| 135 | + enum_field_as_literal=LiteralType.All, |
| 136 | + use_annotated=True, |
| 137 | + reuse_model=True, |
| 138 | + field_constraints=True, |
| 139 | + ) |
| 140 | + |
| 141 | + # Load the generated module |
| 142 | + spec = importlib.util.spec_from_file_location("generated_model", output_path) |
| 143 | + module = importlib.util.module_from_spec(spec) |
| 144 | + sys.modules["generated_model"] = module |
| 145 | + spec.loader.exec_module(module) |
| 146 | + |
| 147 | + # Get the generated model class |
| 148 | + return getattr(module, "Model") |
| 149 | + |
| 150 | + |
151 | 151 | def create_dynamic_tool(
|
152 | 152 | server_name: str,
|
153 | 153 | tool_name: str,
|
@@ -216,24 +216,3 @@ async def is_online(cls) -> bool:
|
216 | 216 | return True
|
217 | 217 |
|
218 | 218 | return DynamicTool
|
219 |
| - |
220 |
| - |
221 |
| -async def main(): |
222 |
| - # Path to the configuration file |
223 |
| - config_path = Path("mcp_config.json") |
224 |
| - |
225 |
| - # Create an instance of MCPClient |
226 |
| - mcp_client = MCPClient(config_path) |
227 |
| - |
228 |
| - # Start the client |
229 |
| - await mcp_client.start() |
230 |
| - |
231 |
| - # Clean up resources |
232 |
| - await mcp_client.cleanup() |
233 |
| - |
234 |
| - while True: |
235 |
| - pass |
236 |
| - |
237 |
| - |
238 |
| -if __name__ == "__main__": |
239 |
| - asyncio.run(main()) |
0 commit comments