File size: 1,921 Bytes
bbf03db
 
55e8764
 
7954b75
e8474ce
a9ab31c
44360b5
7954b75
9da395e
44360b5
 
c0a2494
3431d66
55e8764
9da395e
c0a2494
9da395e
 
b97617e
9da395e
98df7b0
fe9beee
 
98df7b0
 
ad713d9
fe9beee
c4b5200
 
e77d587
 
269a9e5
29da7df
89043fd
 
 
 
e77d587
 
 
89043fd
e77d587
89043fd
 
88c97e7
9da395e
44360b5
 
a33d1c9
44360b5
 
a33d1c9
44360b5
fe9beee
4999f7f
fe9beee
e8474ce
fe9beee
44360b5
 
9a10259
89b3000
5616689
347d46b
bc39ebe
f88d506
9da395e
 
3c9b63e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# References:

# https://www.stockfish.online/docs.php (API)
# https://www.stockfish.online/ (FEN to image)

import re
import gradio as gr
import gradio.utils, requests
from typing import Optional, List, Dict, Any

# MCP server functions

def position_evaluation(fen):
    """Chess position evaluation. Get best move with continuation in UCI notation for chess position in FEN.

    Args:
        fen (str): Chess position in FEN
        
    Returns:
        tuple: (result, error) - Best move with continuation in UCI notation
    """
    is_valid = validate_input(fen)
    
    if not is_valid:
        msg = f"Invalid input"
        gr.Warning(msg)
        return None, msg
    
    print("")
    
    print(f"🤖 Chess position_evaluation: fen={fen}")
    
    url = "https://stockfish.online/api/s/v2.php"
    params = {"fen": fen, "depth": 12}
    
    try:
        response = requests.get(url, params=params, timeout=10)
        response.raise_for_status()
        result = response.json()
        
        print(f"🤖 Chess position_evaluation: result={result}")
        
        return result, None
    
    except requests.exceptions.RequestException as e:
        return None, str(e)

# Helper functions

def _noop(*args, **kwargs):
    pass

gradio.utils.watchfn_spaces = _noop

def validate_input(fen):
    fen_pattern = r'\b([rnbqkpRNBQKP1-8\/]+\s+[wb]\s+(?:-|[KQkq]+)\s+(?:-|[a-h][36])\s+\d+\s+\d+)\b'

    return re.search(fen_pattern, fen)

# Graphical user interface

mcp = gr.Interface(
    fn=position_evaluation,
    inputs =  [gr.Textbox(label = "Forsyth-Edwards Notation (FEN)", value = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", max_length = 75)],
    outputs = [gr.Textbox(label = "Evaluation", interactive = False)],
    title="Stockfish Chess Engine",
    description="Chess position evaluation by top open source chess engine"
)

mcp.launch(mcp_server=True, ssr_mode=False)