libentry 1.22.4__py3-none-any.whl → 1.23__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- libentry/api.py +1 -1
- libentry/mcp/__init__.py +1 -0
- libentry/mcp/api.py +101 -0
- libentry/mcp/client.py +644 -0
- libentry/mcp/service.py +883 -0
- libentry/mcp/types.py +441 -0
- libentry/schema.py +105 -50
- libentry/service/flask.py +1 -1
- {libentry-1.22.4.dist-info → libentry-1.23.dist-info}/METADATA +2 -1
- libentry-1.23.dist-info/RECORD +30 -0
- libentry/service/flask_mcp.py +0 -337
- libentry-1.22.4.dist-info/RECORD +0 -26
- {libentry-1.22.4.dist-info → libentry-1.23.dist-info}/LICENSE +0 -0
- {libentry-1.22.4.dist-info → libentry-1.23.dist-info}/WHEEL +0 -0
- {libentry-1.22.4.dist-info → libentry-1.23.dist-info}/entry_points.txt +0 -0
- {libentry-1.22.4.dist-info → libentry-1.23.dist-info}/top_level.txt +0 -0
- {libentry-1.22.4.dist-info → libentry-1.23.dist-info}/zip-safe +0 -0
libentry/mcp/types.py
ADDED
@@ -0,0 +1,441 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
|
3
|
+
__author__ = "xi"
|
4
|
+
|
5
|
+
import traceback
|
6
|
+
from enum import Enum
|
7
|
+
from typing import Any, Callable, Dict, List, Literal, Optional, Union
|
8
|
+
|
9
|
+
from pydantic import BaseModel, ConfigDict, Field
|
10
|
+
|
11
|
+
JSONObject = Dict[str, Any]
|
12
|
+
JSONList = List[Any]
|
13
|
+
JSONType = Union[JSONObject, JSONList, str, int, float, bool, None]
|
14
|
+
|
15
|
+
|
16
|
+
class MIME(Enum):
|
17
|
+
plain = "text/plain"
|
18
|
+
form = "application/x-www-form-urlencoded"
|
19
|
+
html = "text/html"
|
20
|
+
# object type
|
21
|
+
xml = "application/xml"
|
22
|
+
json = "application/json"
|
23
|
+
# stream type
|
24
|
+
octet_stream = "application/octet-stream"
|
25
|
+
json_stream = "application/json-stream"
|
26
|
+
sse = "text/event-stream"
|
27
|
+
|
28
|
+
|
29
|
+
class SubroutineError(BaseModel):
|
30
|
+
error: str
|
31
|
+
message: str
|
32
|
+
traceback: str
|
33
|
+
|
34
|
+
@classmethod
|
35
|
+
def from_exception(cls, e):
|
36
|
+
err_cls = e.__class__
|
37
|
+
err_name = err_cls.__name__
|
38
|
+
module = err_cls.__module__
|
39
|
+
if module != "builtins":
|
40
|
+
err_name = f"{module}.{err_name}"
|
41
|
+
return cls(
|
42
|
+
error=err_name,
|
43
|
+
message=str(e),
|
44
|
+
traceback=traceback.format_exc()
|
45
|
+
)
|
46
|
+
|
47
|
+
|
48
|
+
class SubroutineResponse(BaseModel):
|
49
|
+
result: Optional[Any] = None
|
50
|
+
error: Optional[SubroutineError] = None
|
51
|
+
|
52
|
+
|
53
|
+
class HTTPOptions(BaseModel):
|
54
|
+
method: Literal["GET", "POST"] = "POST"
|
55
|
+
headers: Optional[Dict[str, str]] = None
|
56
|
+
stream: Optional[bool] = None
|
57
|
+
timeout: int = 15
|
58
|
+
num_trials: int = 5
|
59
|
+
interval: float = 1
|
60
|
+
retry_factor: float = 0.5
|
61
|
+
on_error: Optional[Callable[[Exception], None]] = None
|
62
|
+
|
63
|
+
|
64
|
+
class HTTPRequest(BaseModel):
|
65
|
+
"""HTTP request for a single trial"""
|
66
|
+
|
67
|
+
path: str
|
68
|
+
json_obj: Optional[Dict[str, Any]] = None
|
69
|
+
options: HTTPOptions = Field(default_factory=HTTPOptions)
|
70
|
+
|
71
|
+
|
72
|
+
class SSE(BaseModel):
|
73
|
+
"""Server Send Event"""
|
74
|
+
|
75
|
+
event: str
|
76
|
+
data: Optional[Any] = None
|
77
|
+
|
78
|
+
|
79
|
+
class HTTPResponse(BaseModel):
|
80
|
+
"""HTTP response"""
|
81
|
+
|
82
|
+
status_code: int
|
83
|
+
headers: Dict[str, str]
|
84
|
+
stream: bool
|
85
|
+
content: Any = None
|
86
|
+
|
87
|
+
|
88
|
+
class JSONRPCRequest(BaseModel):
|
89
|
+
jsonrpc: Literal["2.0"]
|
90
|
+
id: Union[str, int]
|
91
|
+
method: str
|
92
|
+
params: Union[Dict[str, Any], None] = None
|
93
|
+
|
94
|
+
model_config = ConfigDict(extra="allow")
|
95
|
+
|
96
|
+
|
97
|
+
class JSONRPCNotification(BaseModel):
|
98
|
+
jsonrpc: Literal["2.0"]
|
99
|
+
method: str
|
100
|
+
params: Union[Dict[str, Any], None] = None
|
101
|
+
|
102
|
+
model_config = ConfigDict(extra="allow")
|
103
|
+
|
104
|
+
|
105
|
+
class JSONRPCError(BaseModel):
|
106
|
+
code: int = 0
|
107
|
+
message: str
|
108
|
+
data: Optional[Any] = None
|
109
|
+
|
110
|
+
@classmethod
|
111
|
+
def from_exception(cls, e):
|
112
|
+
err_cls = e.__class__
|
113
|
+
err_name = err_cls.__name__
|
114
|
+
module = err_cls.__module__
|
115
|
+
if module != "builtins":
|
116
|
+
err_name = f"{module}.{err_name}"
|
117
|
+
return cls(
|
118
|
+
code=0,
|
119
|
+
message=str(e),
|
120
|
+
data={
|
121
|
+
"error": err_name,
|
122
|
+
"message": str(e),
|
123
|
+
"traceback": traceback.format_exc()
|
124
|
+
}
|
125
|
+
)
|
126
|
+
|
127
|
+
|
128
|
+
class JSONRPCResponse(BaseModel):
|
129
|
+
jsonrpc: Literal["2.0"]
|
130
|
+
id: Union[str, int]
|
131
|
+
result: Optional[Any] = None
|
132
|
+
error: Optional[JSONRPCError] = None
|
133
|
+
|
134
|
+
model_config = ConfigDict(extra="allow")
|
135
|
+
|
136
|
+
|
137
|
+
class PaginatedRequest(BaseModel):
|
138
|
+
cursor: Optional[str] = None
|
139
|
+
"""
|
140
|
+
An opaque token representing the current pagination position.
|
141
|
+
If provided, the server should return results starting after this cursor.
|
142
|
+
"""
|
143
|
+
|
144
|
+
|
145
|
+
class PaginatedResult(BaseModel):
|
146
|
+
nextCursor: Optional[str] = None
|
147
|
+
"""
|
148
|
+
An opaque token representing the pagination position after the last returned result.
|
149
|
+
If present, there may be more results available.
|
150
|
+
"""
|
151
|
+
|
152
|
+
|
153
|
+
class Implementation(BaseModel):
|
154
|
+
"""Describes the name and version of an MCP implementation."""
|
155
|
+
|
156
|
+
name: str
|
157
|
+
version: str
|
158
|
+
model_config = ConfigDict(extra="allow")
|
159
|
+
|
160
|
+
|
161
|
+
class RootsCapability(BaseModel):
|
162
|
+
"""Capability for root operations."""
|
163
|
+
|
164
|
+
listChanged: Optional[bool] = None
|
165
|
+
"""Whether the client supports notifications for changes to the roots list."""
|
166
|
+
model_config = ConfigDict(extra="allow")
|
167
|
+
|
168
|
+
|
169
|
+
class SamplingCapability(BaseModel):
|
170
|
+
"""Capability for logging operations."""
|
171
|
+
|
172
|
+
model_config = ConfigDict(extra="allow")
|
173
|
+
|
174
|
+
|
175
|
+
class ClientCapabilities(BaseModel):
|
176
|
+
"""Capabilities a client may support."""
|
177
|
+
|
178
|
+
experimental: Optional[Dict[str, Dict[str, Any]]] = None
|
179
|
+
"""Experimental, non-standard capabilities that the client supports."""
|
180
|
+
sampling: Optional[SamplingCapability] = None
|
181
|
+
"""Present if the client supports sampling from an LLM."""
|
182
|
+
roots: Optional[RootsCapability] = None
|
183
|
+
"""Present if the client supports listing roots."""
|
184
|
+
model_config = ConfigDict(extra="allow")
|
185
|
+
|
186
|
+
|
187
|
+
class PromptsCapability(BaseModel):
|
188
|
+
"""Capability for prompts operations."""
|
189
|
+
|
190
|
+
listChanged: Optional[bool] = None
|
191
|
+
"""Whether this server supports notifications for changes to the prompt list."""
|
192
|
+
model_config = ConfigDict(extra="allow")
|
193
|
+
|
194
|
+
|
195
|
+
class ResourcesCapability(BaseModel):
|
196
|
+
"""Capability for resources operations."""
|
197
|
+
|
198
|
+
subscribe: Optional[bool] = None
|
199
|
+
"""Whether this server supports subscribing to resource updates."""
|
200
|
+
listChanged: Optional[bool] = None
|
201
|
+
"""Whether this server supports notifications for changes to the resource list."""
|
202
|
+
model_config = ConfigDict(extra="allow")
|
203
|
+
|
204
|
+
|
205
|
+
class ToolsCapability(BaseModel):
|
206
|
+
"""Capability for tools operations."""
|
207
|
+
|
208
|
+
listChanged: Optional[bool] = None
|
209
|
+
"""Whether this server supports notifications for changes to the tool list."""
|
210
|
+
model_config = ConfigDict(extra="allow")
|
211
|
+
|
212
|
+
|
213
|
+
class LoggingCapability(BaseModel):
|
214
|
+
"""Capability for logging operations."""
|
215
|
+
|
216
|
+
model_config = ConfigDict(extra="allow")
|
217
|
+
|
218
|
+
|
219
|
+
class ServerCapabilities(BaseModel):
|
220
|
+
"""Capabilities that a server may support."""
|
221
|
+
|
222
|
+
experimental: Optional[Dict[str, Dict[str, Any]]] = None
|
223
|
+
"""Experimental, non-standard capabilities that the server supports."""
|
224
|
+
logging: Optional[LoggingCapability] = None
|
225
|
+
"""Present if the server supports sending log messages to the client."""
|
226
|
+
prompts: Optional[PromptsCapability] = None
|
227
|
+
"""Present if the server offers any prompt templates."""
|
228
|
+
resources: Optional[ResourcesCapability] = None
|
229
|
+
"""Present if the server offers any resources to read."""
|
230
|
+
tools: Optional[ToolsCapability] = None
|
231
|
+
"""Present if the server offers any tools to call."""
|
232
|
+
model_config = ConfigDict(extra="allow")
|
233
|
+
|
234
|
+
|
235
|
+
class InitializeRequestParams(BaseModel):
|
236
|
+
"""Parameters for the initialize request."""
|
237
|
+
|
238
|
+
protocolVersion: Union[str, int]
|
239
|
+
"""The latest version of the Model Context Protocol that the client supports."""
|
240
|
+
capabilities: ClientCapabilities
|
241
|
+
clientInfo: Implementation
|
242
|
+
model_config = ConfigDict(extra="allow")
|
243
|
+
|
244
|
+
|
245
|
+
class InitializeResult(BaseModel):
|
246
|
+
"""After receiving an initialize request from the client, the server sends this."""
|
247
|
+
|
248
|
+
protocolVersion: Union[str, int]
|
249
|
+
"""The version of the Model Context Protocol that the server wants to use."""
|
250
|
+
capabilities: ServerCapabilities
|
251
|
+
serverInfo: Implementation
|
252
|
+
instructions: Optional[str] = None
|
253
|
+
"""Instructions describing how to use the server and its features."""
|
254
|
+
|
255
|
+
|
256
|
+
class InitializedNotification(JSONRPCNotification):
|
257
|
+
"""
|
258
|
+
This notification is sent from the client to the server after initialization has
|
259
|
+
finished.
|
260
|
+
"""
|
261
|
+
|
262
|
+
method: Literal["notifications/initialized"]
|
263
|
+
|
264
|
+
|
265
|
+
class ToolAnnotations(BaseModel):
|
266
|
+
"""
|
267
|
+
Additional properties describing a Tool to clients.
|
268
|
+
|
269
|
+
NOTE: all properties in ToolAnnotations are **hints**.
|
270
|
+
They are not guaranteed to provide a faithful description of
|
271
|
+
tool behavior (including descriptive properties like `title`).
|
272
|
+
|
273
|
+
Clients should never make tool use decisions based on ToolAnnotations
|
274
|
+
received from untrusted servers.
|
275
|
+
"""
|
276
|
+
|
277
|
+
title: Optional[str] = None
|
278
|
+
"""A human-readable title for the tool."""
|
279
|
+
|
280
|
+
readOnlyHint: Optional[bool] = None
|
281
|
+
"""
|
282
|
+
If true, the tool does not modify its environment.
|
283
|
+
Default: false
|
284
|
+
"""
|
285
|
+
|
286
|
+
destructiveHint: Optional[bool] = None
|
287
|
+
"""
|
288
|
+
If true, the tool may perform destructive updates to its environment.
|
289
|
+
If false, the tool performs only additive updates.
|
290
|
+
(This property is meaningful only when `readOnlyHint == false`)
|
291
|
+
Default: true
|
292
|
+
"""
|
293
|
+
|
294
|
+
idempotentHint: Optional[bool] = None
|
295
|
+
"""
|
296
|
+
If true, calling the tool repeatedly with the same arguments
|
297
|
+
will have no additional effect on the its environment.
|
298
|
+
(This property is meaningful only when `readOnlyHint == false`)
|
299
|
+
Default: false
|
300
|
+
"""
|
301
|
+
|
302
|
+
openWorldHint: Optional[bool] = None
|
303
|
+
"""
|
304
|
+
If true, this tool may interact with an "open world" of external
|
305
|
+
entities. If false, the tool's domain of interaction is closed.
|
306
|
+
For example, the world of a web search tool is open, whereas that
|
307
|
+
of a memory tool is not.
|
308
|
+
Default: true
|
309
|
+
"""
|
310
|
+
model_config = ConfigDict(extra="allow")
|
311
|
+
|
312
|
+
|
313
|
+
class ToolProperty(BaseModel):
|
314
|
+
type: Optional[str] = None
|
315
|
+
description: Optional[str] = None
|
316
|
+
|
317
|
+
|
318
|
+
class ToolSchema(BaseModel):
|
319
|
+
type: str = "object"
|
320
|
+
properties: Dict[str, ToolProperty] = {}
|
321
|
+
required: List[str] = []
|
322
|
+
|
323
|
+
|
324
|
+
class Tool(BaseModel):
|
325
|
+
"""Definition for a tool the client can call."""
|
326
|
+
|
327
|
+
name: str
|
328
|
+
"""The name of the tool."""
|
329
|
+
description: Optional[str] = None
|
330
|
+
"""A human-readable description of the tool."""
|
331
|
+
inputSchema: Optional[ToolSchema] = None
|
332
|
+
"""A JSON Schema object defining the expected parameters for the tool."""
|
333
|
+
annotations: Optional[ToolAnnotations] = None
|
334
|
+
"""Optional additional tool information."""
|
335
|
+
model_config = ConfigDict(extra="allow")
|
336
|
+
|
337
|
+
|
338
|
+
class ListToolsResult(PaginatedResult):
|
339
|
+
"""The server's response to a tools/list request from the client."""
|
340
|
+
|
341
|
+
tools: List[Tool]
|
342
|
+
|
343
|
+
|
344
|
+
class TextContent(BaseModel):
|
345
|
+
"""Text content for a message."""
|
346
|
+
|
347
|
+
type: Literal["text"] = "text"
|
348
|
+
text: str
|
349
|
+
"""The text content of the message."""
|
350
|
+
|
351
|
+
model_config = ConfigDict(extra="allow")
|
352
|
+
|
353
|
+
|
354
|
+
class CallToolRequestParams(BaseModel):
|
355
|
+
"""Parameters for calling a tool."""
|
356
|
+
|
357
|
+
name: str
|
358
|
+
arguments: Optional[Dict[str, Any]] = None
|
359
|
+
|
360
|
+
model_config = ConfigDict(extra="allow")
|
361
|
+
|
362
|
+
|
363
|
+
class CallToolResult(BaseModel):
|
364
|
+
"""The server's response to a tool call."""
|
365
|
+
|
366
|
+
content: List[TextContent]
|
367
|
+
isError: bool = False
|
368
|
+
|
369
|
+
|
370
|
+
class Resource(BaseModel):
|
371
|
+
"""A known resource that the server is capable of reading."""
|
372
|
+
|
373
|
+
uri: str
|
374
|
+
"""The URI of this resource."""
|
375
|
+
name: str
|
376
|
+
"""A human-readable name for this resource."""
|
377
|
+
description: Optional[str] = None
|
378
|
+
"""A description of what this resource represents."""
|
379
|
+
mimeType: Optional[str] = None
|
380
|
+
"""The MIME type of this resource, if known."""
|
381
|
+
size: Optional[int] = None
|
382
|
+
"""
|
383
|
+
The size of the raw resource content, in bytes (i.e., before base64 encoding
|
384
|
+
or any tokenization), if known.
|
385
|
+
|
386
|
+
This can be used by Hosts to display file sizes and estimate context window usage.
|
387
|
+
"""
|
388
|
+
|
389
|
+
model_config = ConfigDict(extra="allow")
|
390
|
+
|
391
|
+
|
392
|
+
class ListResourcesResult(PaginatedResult):
|
393
|
+
"""The server's response to a resources/list request from the client."""
|
394
|
+
|
395
|
+
resources: List[Resource]
|
396
|
+
|
397
|
+
|
398
|
+
class ReadResourceRequestParams(BaseModel):
|
399
|
+
"""Parameters for reading a resource."""
|
400
|
+
|
401
|
+
uri: str
|
402
|
+
"""
|
403
|
+
The URI of the resource to read. The URI can use any protocol; it is up to the
|
404
|
+
server how to interpret it.
|
405
|
+
"""
|
406
|
+
|
407
|
+
model_config = ConfigDict(extra="allow")
|
408
|
+
|
409
|
+
|
410
|
+
class ResourceContents(BaseModel):
|
411
|
+
"""The contents of a specific resource or sub-resource."""
|
412
|
+
|
413
|
+
uri: str
|
414
|
+
"""The URI of this resource."""
|
415
|
+
mimeType: Optional[str] = None
|
416
|
+
"""The MIME type of this resource, if known."""
|
417
|
+
|
418
|
+
model_config = ConfigDict(extra="allow")
|
419
|
+
|
420
|
+
|
421
|
+
class TextResourceContents(ResourceContents):
|
422
|
+
"""Text contents of a resource."""
|
423
|
+
|
424
|
+
text: str
|
425
|
+
"""
|
426
|
+
The text of the item. This must only be set if the item can actually be represented
|
427
|
+
as text (not binary data).
|
428
|
+
"""
|
429
|
+
|
430
|
+
|
431
|
+
class BlobResourceContents(ResourceContents):
|
432
|
+
"""Binary contents of a resource."""
|
433
|
+
|
434
|
+
blob: str
|
435
|
+
"""A base64-encoded string representing the binary data of the item."""
|
436
|
+
|
437
|
+
|
438
|
+
class ReadResourceResult(BaseModel):
|
439
|
+
"""The server's response to a resources/read request from the client."""
|
440
|
+
|
441
|
+
contents: List[Union[TextResourceContents, BlobResourceContents]]
|