|
| 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}!" |
0 commit comments