lailaelkoussy commited on
Commit
93ad5fa
·
1 Parent(s): f766c60

add sanitize inputs

Browse files
Files changed (1) hide show
  1. gradio_mcp_space.py +41 -0
gradio_mcp_space.py CHANGED
@@ -30,6 +30,47 @@ except Exception as e:
30
  return func
31
  return decorator
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  # Add parent directory to path
34
  sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'RepoKnowledgeGraphLib'))
35
 
 
30
  return func
31
  return decorator
32
 
33
+
34
+ def _sanitize_value(v):
35
+ if isinstance(v, str):
36
+ return v.strip()
37
+ if isinstance(v, dict):
38
+ return {k: _sanitize_value(val) for k, val in v.items()}
39
+ if isinstance(v, (list, tuple)):
40
+ t = type(v)
41
+ return t(_sanitize_value(x) for x in v)
42
+ return v
43
+
44
+
45
+ def sanitize_inputs(func):
46
+ """Decorator that trims whitespace from all string args/kwargs before calling func."""
47
+ def wrapper(*args, **kwargs):
48
+ new_args = tuple(_sanitize_value(a) for a in args)
49
+ new_kwargs = {k: _sanitize_value(v) for k, v in kwargs.items()}
50
+ return func(*new_args, **new_kwargs)
51
+ # preserve original attributes
52
+ try:
53
+ wrapper.__name__ = func.__name__
54
+ wrapper.__doc__ = func.__doc__
55
+ except Exception:
56
+ pass
57
+ return wrapper
58
+
59
+
60
+ # Wrap the existing `observe` decorator (from langfuse or fallback) so that
61
+ # all observed tools receive sanitized inputs automatically. This avoids
62
+ # having to manually add `@sanitize_inputs` above every `@observe`.
63
+ try:
64
+ _original_observe = observe
65
+ def _observe_with_sanitize(*o_args, **o_kwargs):
66
+ def decorator(f):
67
+ return _original_observe(*o_args, **o_kwargs)(sanitize_inputs(f))
68
+ return decorator
69
+ observe = _observe_with_sanitize
70
+ except Exception:
71
+ # If anything goes wrong, keep the existing observe as-is.
72
+ pass
73
+
74
  # Add parent directory to path
75
  sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'RepoKnowledgeGraphLib'))
76