Skip to content

Commit 134b059

Browse files
HendrikStrobeltjakelorocco
authored andcommitted
silencing some log messages for examples (generative-computing#181)
add MCP example back in own folder and explain how to use in Langflow Co-authored-by: jakelorocco <[email protected]>
1 parent 8374083 commit 134b059

File tree

5 files changed

+123
-1
lines changed

5 files changed

+123
-1
lines changed

docs/examples/agents/react.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
import mellea.stdlib
1414
import mellea.stdlib.base
1515
import mellea.stdlib.chat
16+
from mellea.backends import model_ids
17+
from mellea.helpers.fancy_logger import FancyLogger
1618
from mellea.stdlib.base import ChatContext
1719

20+
FancyLogger.get_logger().setLevel("ERROR")
21+
1822
react_system_template: Template = Template(
1923
"""Answer the user's question as best you can.
2024

docs/examples/mcp/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Write a poem MCP
2+
This is a simple example to show how to write a MCP tool
3+
with Mellea and instruct-validate-repair. Being able to
4+
speak the tool language allows you to integrate with
5+
Claude Desktop, Langflow, ...
6+
7+
See code in [mcp_example.py](mcp_example.py)
8+
9+
## Run the example
10+
You need to install the mcp package:
11+
```bash
12+
uv pip install "mcp[cli]"
13+
```
14+
15+
and run the example in MCP debug UI:
16+
```bash
17+
uv run mcp dev docs/examples/tutorial/mcp_example.py
18+
```
19+
20+
21+
## Use in Langflow
22+
Follow this path (JSON) to use it in Langflow: [https://docs.langflow.org/mcp-client#mcp-stdio-mode](https://docs.langflow.org/mcp-client#mcp-stdio-mode)
23+
24+
The JSON to register your MCP tool is the following. Be sure to insert the absolute path to the directory containing the mcp_example.py file:
25+
26+
```json
27+
{
28+
"mcpServers": {
29+
"mellea_mcp_server": {
30+
"command": "uv",
31+
"args": [
32+
"--directory",
33+
"<ABSOLUTE PATH>/mellea/docs/examples/mcp",
34+
"run",
35+
"mcp",
36+
"run",
37+
"mcp_example.py"
38+
]
39+
}
40+
}
41+
}
42+
```
43+
44+
45+
46+

docs/examples/mcp/mcp_example.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""Example of an MCP server.
2+
3+
You need to install the mcp package:
4+
uv pip install "mcp[cli]"
5+
6+
and run the example in MCP debug UI:
7+
uv run mcp dev docs/examples/tutorial/mcp_example.py
8+
"""
9+
10+
from mcp.server.fastmcp import FastMCP
11+
12+
from mellea import MelleaSession
13+
from mellea.backends import ModelOption, model_ids
14+
from mellea.backends.ollama import OllamaModelBackend
15+
from mellea.stdlib.base import ModelOutputThunk
16+
from mellea.stdlib.requirement import Requirement, simple_validate
17+
from mellea.stdlib.sampling import RejectionSamplingStrategy
18+
19+
# #################
20+
# run MCP debug UI with: uv run mcp dev docs/examples/tutorial/mcp_example.py
21+
# ##################
22+
23+
24+
# Create an MCP server
25+
mcp = FastMCP("Demo")
26+
27+
28+
@mcp.tool()
29+
def write_a_poem(word_limit: int) -> str:
30+
"""Write a poem with a word limit."""
31+
m = MelleaSession(
32+
OllamaModelBackend(
33+
model_ids.HF_SMOLLM2_2B,
34+
model_options={ModelOption.MAX_NEW_TOKENS: word_limit + 10},
35+
)
36+
)
37+
wl_req = Requirement(
38+
f"Use only {word_limit} words.",
39+
validation_fn=simple_validate(lambda x: len(x.split(" ")) < word_limit),
40+
)
41+
42+
res = m.instruct(
43+
"Write a poem",
44+
requirements=[wl_req],
45+
strategy=RejectionSamplingStrategy(loop_budget=2),
46+
)
47+
assert isinstance(res, ModelOutputThunk)
48+
return str(res.value)
49+
50+
51+
@mcp.resource("greeting://{name}")
52+
def get_greeting(name: str) -> str:
53+
"""Get a personalized greeting."""
54+
return f"Hello, {name}!"

docs/examples/mcp/mcp_server.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"mcpServers": {
3+
"mellea_mcp_server": {
4+
"command": "uv",
5+
"args": [
6+
"--directory",
7+
"<ABSOLUTE PATH>/mellea/docs/examples/mcp",
8+
"run",
9+
"mcp",
10+
"run",
11+
"mcp_example.py"
12+
]
13+
}
14+
}
15+
}

docs/examples/mify/rich_table_execute_basic.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
from mellea import start_session
55
from mellea.backends import model_ids
66
from mellea.backends.types import ModelOption
7+
from mellea.helpers.fancy_logger import FancyLogger
78
from mellea.stdlib.docs.richdocument import RichDocument, Table
89

10+
FancyLogger.get_logger().setLevel("ERROR")
11+
912
"""
1013
Here we demonstrate the use of the (internally m-ified) class
1114
RichDocument and Table that are wrappers around Docling documents.
@@ -42,7 +45,7 @@ def cached_doc():
4245
model_id=model_ids.META_LLAMA_3_2_3B,
4346
model_options={ModelOption.MAX_NEW_TOKENS: 500},
4447
)
45-
48+
print("==> Outputs:")
4649
# apply transform on the Table and make sure that the returned object is a Table. Try up to 5 times.
4750
for seed in [x * 12 for x in range(5)]:
4851
table_transformed = m.transform(

0 commit comments

Comments
 (0)