FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-sdk/tests/client/test_logging_callback.py at main · amoycode/python-sdk · GitHub
amoycode
/
python-sdk
Public
forked from
modelcontextprotocol/python-sdk
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-sdk
/
tests
/
client
/
test_logging_callback.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
83 lines (70 loc) · 2.56 KB
Breadcrumbs
python-sdk
/
tests
/
client
/
test_logging_callback.py
Copy path
File metadata and controls
83 lines (70 loc) · 2.56 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from
typing
import
Literal
import
pytest
import
mcp
.
types
as
types
from
mcp
.
shared
.
memory
import
(
create_connected_server_and_client_session
as
create_session
,
)
from
mcp
.
shared
.
session
import
RequestResponder
from
mcp
.
types
import
(
LoggingMessageNotificationParams
,
TextContent
,
)
class
LoggingCollector
:
def
__init__
(
self
):
self
.
log_messages
:
list
[
LoggingMessageNotificationParams
]
=
[]
async
def
__call__
(
self
,
params
:
LoggingMessageNotificationParams
)
->
None
:
self
.
log_messages
.
append
(
params
)
@
pytest
.
mark
.
anyio
async
def
test_logging_callback
():
from
mcp
.
server
.
fastmcp
import
FastMCP
server
=
FastMCP
(
"test"
)
logging_collector
=
LoggingCollector
()
# Create a simple test tool
@
server
.
tool
(
"test_tool"
)
async
def
test_tool
()
->
bool
:
# The actual tool is very simple and just returns True
return
True
# Create a function that can send a log notification
@
server
.
tool
(
"test_tool_with_log"
)
async
def
test_tool_with_log
(
message
:
str
,
level
:
Literal
[
"debug"
,
"info"
,
"warning"
,
"error"
],
logger
:
str
)
->
bool
:
"""Send a log notification to the client."""
await
server
.
get_context
().
log
(
level
=
level
,
message
=
message
,
logger_name
=
logger
,
)
return
True
# Create a message handler to catch exceptions
async
def
message_handler
(
message
:
RequestResponder
[
types
.
ServerRequest
,
types
.
ClientResult
]
|
types
.
ServerNotification
|
Exception
,
)
->
None
:
if
isinstance
(
message
,
Exception
):
raise
message
async
with
create_session
(
server
.
_mcp_server
,
logging_callback
=
logging_collector
,
message_handler
=
message_handler
,
)
as
client_session
:
# First verify our test tool works
result
=
await
client_session
.
call_tool
(
"test_tool"
, {})
assert
result
.
isError
is
False
assert
isinstance
(
result
.
content
[
0
],
TextContent
)
assert
result
.
content
[
0
].
text
==
"true"
# Now send a log message via our tool
log_result
=
await
client_session
.
call_tool
(
"test_tool_with_log"
,
{
"message"
:
"Test log message"
,
"level"
:
"info"
,
"logger"
:
"test_logger"
,
},
)
assert
log_result
.
isError
is
False
assert
len
(
logging_collector
.
log_messages
)
==
1
assert
logging_collector
.
log_messages
[
0
]
==
LoggingMessageNotificationParams
(
level
=
"info"
,
logger
=
"test_logger"
,
data
=
"Test log message"
)
Back
|
FazBrowse Home
|
New Git URL