Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a82f4ac
fix: close the session when read_stream closed
terry-xiaoyu Mar 31, 2025
9d248d3
support mqtt transport
terry-xiaoyu Mar 30, 2025
f4fa9cc
change user property name mcp_client_id to mcp-client-id
terry-xiaoyu Apr 1, 2025
6fe4401
separate the mqtt transport to base class and sub classes
terry-xiaoyu Apr 1, 2025
4b95182
add mqtt transport for mcp clients
terry-xiaoyu Apr 3, 2025
5bce93f
rename service to server
terry-xiaoyu Apr 5, 2025
a78cf47
refactor some callbacks of mcp client
terry-xiaoyu Apr 5, 2025
d4520ee
fix: session crashes on response_stream_reader EOF
terry-xiaoyu Apr 5, 2025
17e8adc
improve client_apis_demo.py
terry-xiaoyu Apr 5, 2025
6edbbf8
add server-id into contorl topic
terry-xiaoyu Apr 17, 2025
3fb9d85
support broker suggested MCP server name
terry-xiaoyu Apr 17, 2025
b755b90
log mqtt settings
terry-xiaoyu May 14, 2025
0c755d2
add server-id into the rpc topic; change client_id_prefix to client_id
terry-xiaoyu May 23, 2025
09030e5
change MQTT-CLIENT-ID to MCP-MQTT-CLIENT-ID
terry-xiaoyu May 24, 2025
617797b
fix: got incorrect server_name in clients
terry-xiaoyu May 24, 2025
c572d12
wait for connection success when start a mqtt client
terry-xiaoyu Jun 6, 2025
2290987
fix: handle connection refused exceptions
terry-xiaoyu Jun 7, 2025
7277afd
use the same topic to send list_changed and resource updated notifica…
terry-xiaoyu Jun 19, 2025
b9ac0b5
mcp client support disconnect a specific mcp server
terry-xiaoyu Jun 20, 2025
c476a33
fix: Pop client session if it failed to initialized
HJianBo Jun 25, 2025
39c5014
support broker suggested server-name-filters
terry-xiaoyu Jul 21, 2025
f6d3370
fix: make the params backward-compatible
terry-xiaoyu Jul 22, 2025
29b9dae
fix: mqtt mcp client crash on clean up server_ids
terry-xiaoyu Sep 3, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions examples/clients/mqtt-clients/client_apis_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import logging

import anyio

import mcp.client.mqtt as mcp_mqtt
from mcp.shared.mqtt import configure_logging

configure_logging(level="DEBUG")
logger = logging.getLogger(__name__)

async def on_mcp_server_discovered(client, server_name):
logger.info(f"Discovered {server_name}, connecting ...")
await client.initialize_mcp_server(server_name)

async def on_mcp_connect(client, server_name, connect_result):
capabilities = client.get_session(server_name).server_info.capabilities
logger.info(f"Capabilities of {server_name}: {capabilities}")
if capabilities.prompts:
prompts = await client.list_prompts(server_name)
logger.info(f"Prompts of {server_name}: {prompts}")
if capabilities.resources:
resources = await client.list_resources(server_name)
logger.info(f"Resources of {server_name}: {resources}")
resource_templates = await client.list_resource_templates(server_name)
logger.info(f"Resources templates of {server_name}: {resource_templates}")
if capabilities.tools:
toolsResult = await client.list_tools(server_name)
tools = toolsResult.tools
logger.info(f"Tools of {server_name}: {tools}")

async def on_mcp_disconnect(client, server_name):
logger.info(f"Disconnected from {server_name}")

async def main():
async with mcp_mqtt.MqttTransportClient(
"test_client",
auto_connect_to_mcp_server = True,
on_mcp_server_discovered = on_mcp_server_discovered,
on_mcp_connect = on_mcp_connect,
on_mcp_disconnect = on_mcp_disconnect,
mqtt_options = mcp_mqtt.MqttOptions(
host="broker.emqx.io",
)
) as client:
client.start()
while True:
logger.info("Other works while the MQTT transport client is running in the background...")
await anyio.sleep(10)

if __name__ == "__main__":
anyio.run(main)
27 changes: 27 additions & 0 deletions examples/fastmcp/mqtt_simple_echo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
FastMCP Echo Server
"""

from mcp.server.fastmcp import FastMCP
from mcp.shared.mqtt import MqttOptions

# Create server
mcp = FastMCP(
"demo_server/echo",
log_level="DEBUG",
)

mcp.settings.mqtt_options = MqttOptions(
host="broker.emqx.io",
verify_connack_properties=True, # Change to False if broker is Mosquitto
)


@mcp.tool()
def echo(text: str) -> str:
"""Echo the input text"""
return text


if __name__ == "__main__":
mcp.run(transport="mqtt")
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies = [
"uvicorn>=0.31.1; sys_platform != 'emscripten'",
"jsonschema>=4.20.0",
"pywin32>=310; sys_platform == 'win32'",
"paho-mqtt>=2.1.0",
]

[project.optional-dependencies]
Expand All @@ -59,6 +60,9 @@ dev = [
"pytest-pretty>=1.2.0",
"inline-snapshot>=0.23.0",
"dirty-equals>=0.9.0",
"typer>=0.17.4",
"python-dotenv>=1.1.1",
"websockets>=15.0.1",
]
docs = [
"mkdocs>=1.6.1",
Expand Down Expand Up @@ -134,7 +138,7 @@ max-returns = 13 # Default is 6
max-statements = 102 # Default is 50

[tool.uv.workspace]
members = ["examples/servers/*", "examples/snippets"]
members = ["examples/servers/*", "examples/snippets", "examples/clients/mqtt-clients/smart-home"]

[tool.uv.sources]
mcp = { workspace = true }
Expand Down
Loading