channel.py¶
clew/mcp_server/channel.py
Push a wiki comment into the Claude Code session as a channel event (2.1, opt-in).
attach_loop
None clew.mcp_server.channel.Channel.attach_loop(self)
Take the running event loop's token (call from inside the loop).
Source · 4 lines
def attach_loop(self) -> None:
"""@brief Loop token. @return None."""
self.token = anyio.lowlevel.current_token()
Activity · 1 steps
Requirements
enabled
bool clew.mcp_server.channel.enabled()
Whether the server was started with the channel switch on.
Source · 5 lines
def enabled() -> bool:
"""@brief Switch. @return Bool."""
return os.environ.get(CHANNEL_ENV) == "1"
Activity · 1 steps
Requirements
experimental_capabilities
dict[str, dict[str, Any]] clew.mcp_server.channel.experimental_capabilities()
The experimental capabilities to declare at initialize.
Source · 5 lines
def experimental_capabilities() -> dict[str, dict[str, Any]]:
"""@brief Capabilities. @return Dict."""
return {CAPABILITY: {}} if enabled() else {}
Activity · 1 steps
Requirements
push_from_thread
bool clew.mcp_server.channel.Channel.push_from_thread(self, str repo, dict[str, Any] comment)
Push from a non-loop thread (the hub's), never raising.
clew/mcp_server/channel.py:106
Source · 18 lines
def push_from_thread(self, repo: str, comment: dict[str, Any]) -> bool:
"""A FAILED PUSH COSTS A LOG LINE, NEVER THE COMMENT: it is already stored, and the hook and
`index(action='comments')` deliver it regardless.
@brief Push from a thread.
@return Sent.
"""
if self.token is None:
logger.info(
"wiki comment %s: channel push skipped, no event loop attached", comment.get("id")
)
return False
try:
return bool(anyio.from_thread.run(self.send, repo, comment, token=self.token))
except Exception as exc: # noqa: BLE001 — a push must never fail the HTTP request
logger.warning("wiki comment %s: channel push failed: %s", comment.get("id"), exc)
return False
Activity · 8 steps
Requirements
remember
None clew.mcp_server.channel.Channel.remember(self, Any ctx)
Remember the session a tool call carried.
Source · 6 lines
def remember(self, ctx: Any) -> None:
"""@brief Remember. @return None."""
session = getattr(ctx, "session", None)
if session is not None:
self.session = session
Activity · 3 steps
Requirements
send
bool clew.mcp_server.channel.Channel.send(self, str repo, dict[str, Any] comment)
Send one comment as a channel notification, when the session can receive it.
clew/mcp_server/channel.py:130
Source · 25 lines
async def send(self, repo: str, comment: dict[str, Any]) -> bool:
"""`meta` KEYS ARE LETTERS, DIGITS AND UNDERSCORES: Claude Code drops any other key silently.
@brief Send.
@return Sent.
"""
if self.session is None:
logger.info(
"wiki comment %s: channel push skipped, no session seen yet", comment.get("id")
)
return False
version = protocol_version(self.session)
if version in MODERN_PROTOCOL_VERSIONS:
logger.info(
"wiki comment %s: channel inactive on MCP %s (Claude Code registers channels only on "
"earlier revisions); the hook and index(action='comments') still deliver it",
comment.get("id"),
version,
)
return False
await raw_notify(self.session, METHOD, _params(repo, comment))
logger.info("wiki comment %s pushed on the channel", comment.get("id"))
return True