2
2
from typing import Any
3
3
import httpx
4
4
from mcp .server .fastmcp import FastMCP
5
- import asyncio
6
- from starlette .responses import PlainTextResponse
7
5
8
6
SLACK_API_BASE = "https://slack.com/api"
9
7
MCP_TRANSPORT = os .environ .get ("MCP_TRANSPORT" , "stdio" )
13
11
"slack" , settings = {"host" : "127.0.0.1" if MCP_TRANSPORT == "stdio" else "0.0.0.0" }
14
12
)
15
13
16
- # Global metrics store
17
- metrics = {
18
- 'get_channel_history_total' : 0 ,
19
- 'post_message_total' : 0 ,
20
- 'post_command_total' : 0 ,
21
- 'add_reaction_total' : 0 ,
22
- 'whoami_total' : 0 ,
23
- 'join_channel_total' : 0 ,
24
- }
25
- metrics_lock = asyncio .Lock ()
26
14
27
15
async def make_request (
28
16
url : str , payload : dict [str , Any ] | None = None
@@ -61,8 +49,6 @@ async def log_to_slack(message: str):
61
49
@mcp .tool ()
62
50
async def get_channel_history (channel_id : str ) -> str :
63
51
"""Get the history of a channel."""
64
- async with metrics_lock :
65
- metrics ['get_channel_history_total' ] += 1
66
52
await log_to_slack (f"Getting history of channel <#{ channel_id } >" )
67
53
url = f"{ SLACK_API_BASE } /conversations.history"
68
54
payload = {"channel" : channel_id }
@@ -76,8 +62,6 @@ async def post_message(
76
62
channel_id : str , message : str , thread_ts : str = "" , skip_log : bool = False
77
63
) -> str :
78
64
"""Post a message to a channel."""
79
- async with metrics_lock :
80
- metrics ['post_message_total' ] += 1
81
65
if not skip_log :
82
66
await log_to_slack (f"Posting message to channel <#{ channel_id } >: { message } " )
83
67
await join_channel (channel_id , skip_log = skip_log )
@@ -94,8 +78,6 @@ async def post_command(
94
78
channel_id : str , command : str , text : str , skip_log : bool = False
95
79
) -> str :
96
80
"""Post a command to a channel."""
97
- async with metrics_lock :
98
- metrics ['post_command_total' ] += 1
99
81
if not skip_log :
100
82
await log_to_slack (
101
83
f"Posting command to channel <#{ channel_id } >: { command } { text } "
@@ -110,8 +92,6 @@ async def post_command(
110
92
@mcp .tool ()
111
93
async def add_reaction (channel_id : str , message_ts : str , reaction : str ) -> str :
112
94
"""Add a reaction to a message."""
113
- async with metrics_lock :
114
- metrics ['add_reaction_total' ] += 1
115
95
await log_to_slack (
116
96
f"Adding reaction to message { message_ts } in channel <#{ channel_id } >: :{ reaction } :"
117
97
)
@@ -124,8 +104,6 @@ async def add_reaction(channel_id: str, message_ts: str, reaction: str) -> str:
124
104
@mcp .tool ()
125
105
async def whoami () -> str :
126
106
"""Checks authentication & identity."""
127
- async with metrics_lock :
128
- metrics ['whoami_total' ] += 1
129
107
await log_to_slack ("Checking authentication & identity" )
130
108
url = f"{ SLACK_API_BASE } /auth.test"
131
109
data = await make_request (url )
@@ -135,8 +113,6 @@ async def whoami() -> str:
135
113
@mcp .tool ()
136
114
async def join_channel (channel_id : str , skip_log : bool = False ) -> str :
137
115
"""Join a channel."""
138
- async with metrics_lock :
139
- metrics ['join_channel_total' ] += 1
140
116
if not skip_log :
141
117
await log_to_slack (f"Joining channel <#{ channel_id } >" )
142
118
url = f"{ SLACK_API_BASE } /conversations.join"
@@ -145,12 +121,5 @@ async def join_channel(channel_id: str, skip_log: bool = False) -> str:
145
121
return data .get ("ok" )
146
122
147
123
148
- @mcp .custom_route ("/metrics" , methods = ["GET" ])
149
- async def metrics_endpoint (request ):
150
- async with metrics_lock :
151
- lines = [f"{ k } { v } " for k , v in metrics .items ()]
152
- return PlainTextResponse ("\n " .join (lines ) + "\n " , media_type = "text/plain" )
153
-
154
-
155
124
if __name__ == "__main__" :
156
125
mcp .run (transport = MCP_TRANSPORT )
0 commit comments