Files
Pietro Zullo be9942a4d4 feat/python/server (#559)
* feat: server first implementation

* docs: docs for server and images

* chore: locked langchain dependencies in pyproject.toml (#321)

* python/release/1.3.13

* catch uvicorn logs && log session id

* fix debug logic

* docs: logging update

* fix: icon of elicitation

* fix installation paths for local clones

* docs: minor restructuring

* docs: reorganization, killed non existing pages

* docs: beta tag on server

* chore: example servers

* feat: inspector autopopulated and client only

* feat: enhance MCP logging system with customizable formats and improved middleware

* feat: implement weather alert and forecast tools in FastMCP example server

* mcp path configurable

* fix inspector

* export as FastMCP too

* chore: update fastmcp migration example

* lock inspector version

* type runner

* unhide docs and remove dangers

* migration diff

* docs and context example

* export context

* logging improvement

* pretty JSON RPC

* autoConnect url param in inspecotr

* move logging

* type check

* Server Router

* show response logs

* fallback tool name to function name in router registration

* compatibility export

* Introduced ty and prek as type checker and precommit engine respectively - moved ruff config in pyproject toml

* add ty check

* ty runs only on staged files

* ty configuration fix

* move startup info in logging

* remove bug from action

* update claude rules

* fix resource serialization

* comments on inspector errors

* abstracted runner transports

* remove redunant call for network

* fix ip getter

* add inspector mcp path

* response limit logging

* disable ty check in action

* moved transport type to types

* support sse

* show transport type in logs

* change default transport

* refactored tests to use mcp server

* removed fastmcp

* file restructuring

* test table

* sampling, elicitation, tool disabling enabling

* deprecte sse server side

* simplified report

* fix action

* remove sse deprecated test

* move tests files to allow for server integration tests

* docs

* reorg integration test files

* api-reference-doxs

* fix broken links

* telemetry

* remove feature useage from scarf
2025-12-01 14:12:16 +01:00

55 lines
1.9 KiB
Python

import inspect
import logging
import socket
from typing import Any, get_type_hints
logger = logging.getLogger(__name__)
def estimate_tokens(text: str) -> int:
"""Rough estimate of token count (approximately 4 characters per token)."""
return max(1, len(str(text)) // 4)
def get_local_network_ip() -> str | None:
"""Get the local network IP address."""
try:
# Connect to a remote address to determine local IP
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.connect(("8.8.8.8", 80))
return s.getsockname()[0]
except OSError as exc:
logger.debug("Failed to determine local network IP: %s", exc)
return None
def get_return_type(func_or_callable) -> type:
"""Get the return type annotation from a function or callable class."""
if inspect.isclass(func_or_callable):
# It's a class, get return type from __call__ method
if callable(func_or_callable):
call_method = func_or_callable.__call__
return get_return_type(call_method)
return Any
elif (
callable(func_or_callable)
and not inspect.isfunction(func_or_callable)
and not inspect.ismethod(func_or_callable)
and not inspect.isbuiltin(func_or_callable)
):
# It's a callable class instance, get return type from __call__ method
return get_return_type(func_or_callable.__call__)
else:
# It's a regular function
try:
# Try get_type_hints first (handles forward references)
hints = get_type_hints(func_or_callable)
return hints.get("return", Any)
except (NameError, AttributeError):
# Fallback to inspect.signature
try:
sig = inspect.signature(func_or_callable)
return sig.return_annotation if sig.return_annotation != inspect.Signature.empty else Any
except (ValueError, TypeError):
return Any